• C

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(). I now know that the problem is that i am trying to assign an int to a struct ptr, but how do i fix it?
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);
         
}


Gizmo1Asked:
Who is Participating?
 
ntdragonConnect With a Mentor Commented:
try mine i changed yours

Structure definitions:
#define NAMESIZE 20

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

typedef
struct item
{
int next;
struct_person details;
}struct_item;

typedef
struct list
{
struct item * head;
struct item * tail;
}struct_list;


Function prototypes:
struct_item * search(struct_list *l, int id);
int insert(struct_list *l, struct_person data);
int delete(struct_list *l,int i);


//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)
        {
                return(p);
        }
if (p==NULL) return(NULL);
}

int insert(struct_list *l, struct_person data)
{
struct_item *item;

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;
        l->tail=item;
}
else
{
        l->tail=item;
}
         
return (1);
}

int delete(struct_list *l,int id)
{
struct_item *temp;
struct_item *prev;

if((temp=search(l,id))==NULL)
   return 0;

if(l->head==l-tail){
   l->head=NULL;
   l->tail=NULL;
   free(temp);
   return 1;
}
if(temp==l->head){
   l->head=l->head->next;
   free(temp);
   return 1;
}

for(prev=l->head;prev->next==temp;
prive=prev->next);

prev->next=temp->next;
free(temp);
return 1;
         
}

0
 
_lychee_Commented:
u should note that
the p=NULL in ur for statement is almost definitely wrong... i think u mean p!=NULL?

as for ur errors, l->head is an int, and u're trying to assign it to a *...
0
 
csideCommented:
The error 'assignment makes integer from pointer without cast' is occuring because you are assigning a pointer to an integer variable or structure member. For example on the line

l->head=item;

In this statement the item is a pointer to a struct_item and head is an integer. You may well be seeing the converse error where you assign the integer back to a pointer.

You are correctly creating pointers to traverse your lists so my advice is to look back at your structures and think about the types you need to use to create the links.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.