[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

linked lists using dynamic memory allocation

Posted on 2000-04-30
3
Medium Priority
?
298 Views
Last Modified: 2010-04-15
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);
         
}


0
Comment
Question by:Gizmo1
3 Comments
 
LVL 1

Accepted Solution

by:
ntdragon earned 200 total points
ID: 2763450
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
 
LVL 2

Expert Comment

by:_lychee_
ID: 2764032
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
 

Expert Comment

by:cside
ID: 2774294
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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