Link to home
Start Free TrialLog in
Avatar of Gizmo1
Gizmo1

asked on

linked lists using dynamic memory allocation

The purpose of this program is to create a linked list using dynamic memory allocation to store a person's id,firstname,surname and age.I have one error which is repeated several times throughout the list.c program. The error is ' assignment makes integer from pointer without cast'. I have no idea what this means.
One place that it occurs is in the search() function on the line:
for (p=l->head;p!=NULL;p=p->next).
It also occurs in my delete() and insert().
Can anyone give me any hints as to the problem?
I have written functions for initialise and print and these compile fine.

Structure definitions:
#define NAMESIZE 20

struct person
{
int id;
char firstname[NAMESIZE];
char surname[NAMESIZE];
int age;
};

struct item
{
int next;
struct person details;
};

struct list
{
int head;
int tail;
struct item *item;
};


Function prototypes:
struct item * search(struct list *l, int);
struct item * insert(struct list *l, struct person);
int delete(struct list *,int);


Functions:
struct item * search(struct list *l, int id)
{
struct item *p;
for(p=l->head;p=NULL;p=p->next)
        if(p->details.id==id)
        {
                break;
                return(p);
        }
        else
        {
                return(NULL);
        }
}

struct item * insert(struct list *l, struct person data)
{
struct item *item;
struct item *ptr;

item=(struct item*)malloc(sizeof(struct item)); /*get new item*/
item->details.id=data.id;                     /*assign data*/
strcpy(item->details.firstname,data.firstname);
strcpy(item->details.surname,data.surname);
item->details.age=data.age;                  
item->next=NULL;                                /*initialise next pointer*/

if(l->head==NULL)
        &l->head=item;
else
{
/*find list item in list which is the first one with a null pointer*/
        for(ptr=l->head;ptr->next=NULL;ptr=ptr->next)  /*this is a null statement*/
                ptr->next=item;
}
       

return (item);
}

int delete(struct list *l,int id)
{
struct item *temp;

if(search(l,id)!=NULL)   /*item found in list, continue deleting*/

{
        if(l->head==NULL)   /*delete the first item*/
        {
                temp=l->head;
                l->head=temp->next;
        }

        else
        {
                temp=temp->next;     /*delete the next item*/
                temp->next=temp->next;
        }
               

        free (temp);
        return(1);     /*return 1 to indicate successful delete*/
}
else   /*item not found, return 0 */
return(0);
       
}



ASKER CERTIFIED SOLUTION
Avatar of sa9813
sa9813

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gizmo1
Gizmo1

ASKER

I know what the problem is(trying to assign an int to a struct pointer) but how do I fix it?
Thanks.
I did not check the logic of you program, just for the errors you were asking about. But I think that you would want the head, next, tail to be pointers, used to navigate throug your structure. There is no logic having them as int's.