0

I got a struct defining a node for a tree data structure:

struct Node {
  int data;
  struct Node *children[10];
}

Given that children is NOT a dynamic array, I would like to initialize each pointer of children to NULL, but what follows doesn't work:

struct Node {
  int data;
  struct Node *children[10]={NULL};
}

Is there any workaround?

4
  • 2
    Please include your actual code in the question. struct *Node children[10]; is a syntax error. (Struct members cannot have initializers.) Commented May 4, 2015 at 16:12
  • If by some chance you are working with a C++ compiler, you can add a constructor to your struct which initializes all the member fields. Commented May 4, 2015 at 16:14
  • Ok my syntax error was actually a typo. Corrected. And no, I cannot use a C++ compiler. Commented May 4, 2015 at 16:32
  • Please accept my apologies, after my last comment I was left without internet connection. All useful answers, I upvoted them all. Probably I should go with designated initializers. Commented May 5, 2015 at 22:25

3 Answers 3

3

This

 struct *Node children[10];  

is wrong. I would not even compile. It should be

 struct Node *children[10];  

To initialize all elements of member children to NULL you can use designated initializers.

struct Node {
  int data;
  struct Node *children[10];
} node = {.children = {NULL}};
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

memset is not guaranteed to set pointers to NULL; a null pointer can legally have a non-zero representation. It's likely to work on most systems.
struct Node my_node = {}; is not C.
2
struct Node {
  int data;
  struct Node *children[10];
} a = {.children = {0}};

a is a struct Node object with all element of children member initialized to a null pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.