You can't initialize data in the description of the struct
because no memory has been allocated yet.
Let's look at the two styles of allocation you'll see:
The Stack
struct Node my_node = {
0,
{NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}
};
Or, because unlisted values will default to 0
...
struct Node my_node = {};
The Heap:
Here, are only option is to null out the memory. We could use calloc()
to do this since the memory it returns is zeroed out.
struct Node *my_node = calloc(1, sizeof(*my_node));
Or, we can explicitly use memset()
:
struct Node *my_node = malloc(sizeof(*my_node));
memset(my_node, 0, sizeof(*my_node));
Notes:
I'm generally assuming that NULL == 0
. This isn't necessarily true. If you'd like to read more about these (mostly) historical systems: When was the NULL macro not 0?
If you're on one of those systems, or you're concerned about your code working on those platforms, then I would recommend using the first method (and most explicit) method that I described. It will work on all platforms.