C end structure list

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error C end structure list on this date .

I want to make a list of structures in C. In my program, I read lines from a file and store information in structures (one structure per line). So my code is something like this:

struct myStruct *myStructList;

// Here is the parsing thing

And, at the end of the parsing, I try to do this:

myStructList[i] = NULL;

Where i is the next free position in the list (which has been already reserved with realloc).

This is the error I’m getting.

incompatible types when assigning to type ‘struct myStruct’ from type ‘void *’

It appears I cannot do the NULL thing. My question is: what can I put there if it isn’t NULL? How do I know the list has finished? I know I can keep a counter with the number of elements in the list, but I would like to avoid that.

Answer

In C, unlike java – the array is holding elements by value, and not by reference.

So, you are trying to assign a pointer (address) to a struct, and this operation is undefined – so you get this error.

If you want to set an element to NULL, you should declare the array as struct mySTruct*[], and assign pointers instead of values.