I've tried and distill the following code as much as possible:
#include <stdlib.h>
typedef struct item{
struct item *next;
}item;
void addItem(item *list)
{
item *newItem = list;
while(newItem->next != NULL){
newItem = newItem->next;
}
newItem->next = malloc(sizeof(item));
newItem->next->next = NULL;
}
int main()
{
item *groceryList = NULL;
groceryList = malloc(sizeof(item));
if(groceryList == NULL)
return 1;
groceryList->next = NULL;
addItem(groceryList);
return 0;
}
This compiles fine. But changing the struct declaration to (or any combination of the changes):
typedef struct{ /*Removed "item"*/
item *next; /*Removed "struct"*/
}item;
Causes the following errors when compiling:
structpointertest.c:11:11: warning: assignment from incompatible pointer type structpointertest.c:15:15: error: request for member 'next' in something not a structure or union
I don't understand what in the struct declaration is causing this issue? Does it have something to do with the fact I'm using a nested struct?
Thanks.
itemis at the declaration lineitem *next.