Home > Net >  Lists and Pointers in C
Lists and Pointers in C

Time:01-09

I have a problem with lists and pointers, let me explain. let's define a list like this:

typedef struct list *LIST;
typedef struct node *link;
struct list{link head; int n;};
struct node{Item val;link next;};

where n is the number of nodes and LIST is a pointer to struct list (I have to do that because I want to make an opaque pointer to the list, in my homework the LIST pointer would go in the .h and the structs would go in the .c but that's not the problem, and I know I should avoid declaring pointer like that).

The Item type is also declared via an opaque pointer, so I have something like this:

typedef struct info *Item;
struct info{char *name;int N};

my problem is that I don't understand how to insert stuff in this lists. (so I would like to add an Item type to the lists, but I can't because Item is a pointer so, for example if a try to do this:

//the lists is already initialized and let's say we want to add 3 nodes
Item x=malloc(sizeof(*x));`
x->name=calloc(10,sizeof(char));
for(int i=0;i<3;i  ){
   fscanf("%s",x->name);
   fscanf("%d",x->N);//this is a random number
   ListInsert(L,x);
}

this is what i have in ListInsert():

void ListInsert(LIST L, Item x){
link z,p;
if(L->head==NULL)
   L->head=newNode(x,L->head);
else{
for(z=L->head->next, p=L->head;z!=NULL;p=x, z=z->next);//i know a tail would help 
p->next=newNode(x,z);
}
}

And this is what I have in newNode():

link newNode(item x,link next){
link z=malloc(sizeof(*z));//should control the allocation was successful I know
z->val=x;
z->next=next;
return z;
}

Whenever I modify the value x, I'm actually modifying what the head and everything points to, that's my problem, what could be a solution? maybe make an array? pointers can sometimes be so hard to understand, for example should I allocate z->val->name?

CodePudding user response:

When you say ...

Whenever I modify the value x, I'm actually modifying what the head and everything points to, that's my problem, what could be a solution?

... I think you're talking about this code:

Item x=malloc(sizeof(*x));`
x->name=calloc(10,sizeof(char));
for(int i=0;i<3;i  ){
   fscanf("%s",x->name);
   fscanf("%d",x->N);//this is a random number
   ListInsert(L,x);
}

Indeed, you have allocated only one struct info and assigned x to point to it. You have added that one struct info to your linked list three times, and also modified it several times.

Supposing that your objective is to add three distinct objects to the list, the solution starts with allocating three distinct objects (else where would they come from?). Since each one has a pointer to a dynamically allocated array, you will also want to allocate a separate array for each of those. The easiest way to achieve that would be simply to move the allocations into the loop:

for (int i = 0; i < 3; i  ) {
   Item x = malloc(sizeof(*x));
   x->name = calloc(10, sizeof(char));
   fscanf("%s", x->name);
   fscanf("%d", x->N);  //this is a random number
   ListInsert(L, x);
}

If you are permitted to modify the structures involved then you could also consider making the name element of struct info an array of suitable length instead of a pointer. That's a little less flexible, but it would mean that you need only one allocation for each item, not two.

  •  Tags:  
  • Related