Solved

linked lists using dynamic memory allocation

Posted on 2000-04-29
3
250 Views
Last Modified: 2012-05-04
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);
       
}



0
Comment
Question by:Gizmo1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Accepted Solution

by:
sa9813 earned 50 total points
ID: 2763320
I have commented in the code where the problems are, the ones you are reffering to in your question. Basically what you are trying to do is to assing an int (l->head) to a pointer of some kind. Maybe this gets you on the right track.

#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;
};



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



struct item * search(struct list *l, int id) {
struct item *p;

//p = l->head   p is a struct item pointer
//in your struct list head is an int. You are here
//assigning an int to a struct item pointer!

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 {

            //same thing goes here, you are trying to assign
            //a int to a struct item pointer
            //ptr = l->head!
        for(ptr=l->head;ptr->next=NULL;ptr=ptr->next)  
                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*/
        {

                        //same thing here, temp = l->head!

                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);
         
}





0
 

Author Comment

by:Gizmo1
ID: 2763401
I know what the problem is(trying to assign an int to a struct pointer) but how do I fix it?
Thanks.
0
 

Expert Comment

by:sa9813
ID: 2763456
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux context switch  - loop takes long time to process 6 156
Intel debugger, idb, only seeing .c and .h, but no .f90 1 540
Problem to scan all sheets 3 135
MySQL recovery 7 61
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question