Solved

linked lists using dynamic memory allocation

Posted on 2000-04-30
3
265 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 50 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now