infinity-1 infinity-1 - 26 days ago 9
C Question

Error when trying to move to next element of a Linked List

I am dealing with linked lists, and am having issues moving to the next element of the Linked List. the error that I get is:

error: incompatible types when assigning to type 'item' from type 'struct item *


Here is the code:

typedef struct item
{
float size;
struct item *next;
} item;

item head, curr, tail;


...

head.next = (item*) malloc(sizeof(item));
curr = head.next;


Thanks for your help.

Answer

Based in your description, you want to make curr the next node of the linked list (?).

In that case, you should assign the memory direction of curr to head.next. How, you may ask ?:

item head, curr;   // declaration of variables

head.size = 8;
curr.size = 5;

head.next = &curr; // making 'curr' the next node

Now, in the memory you will have something like this (memory addresses are just arbitraries numbers):

Memory example

NOTE: Pointers keep memory addresses. That's why you assign them the return value of malloc(), which give the memory direction of an available "memory chunk" with the expecified length.