0

I know C is very finnicky about file level initialization. Or rather I just don't know what constant expression means yet.

What I want to do is initialize a node (aka struct node) with all null pointers.

//Trie node definition
typedef struct node{
    bool is_word;
    struct node* next[27]; //27 for the valid number of chars

}node;


struct node* empties[27];
node empty = {.is_word = 0, .next = empties};

dictionary.c:24:33: error: incompatible pointer types initializing 'struct node *' with an
      expression of type 'struct node *[27]' [-Werror,-Wincompatible-pointer-types]
node empty = {.is_word=0,.next =empties};
                                ^~~~~~~
dictionary.c:24:33: error: suggest braces around initialization of subobject
      [-Werror,-Wmissing-braces]
node empty = {.is_word=0,.next =empties};

I'm getting an error when I try to initialize this. I would also try manually initializing the members but 27 indexes makes that very tedious. Is there a way to loop initialize at the file level?

Barry
  • 286,269
  • 29
  • 621
  • 977
veta
  • 716
  • 9
  • 22

2 Answers2

1

Try node empty = {0, {0}};.

This is an effective way to initialize structs and arrays, or as in this case, a struct containing an array.

How to initialize all members of an array to the same value? has more on array initialization. But you can also embed the initializer into a struct, as shown here.

Community
  • 1
  • 1
Topological Sort
  • 2,733
  • 2
  • 27
  • 54
  • Worked like a charm. I also found the global zero default initialization does carry to struct members so "node empty;" works too. Your method lets me set any values though so I will use it in the future. Thanks :) – veta May 06 '15 at 18:04
  • Great! I think you should be cautious of not giving explicit initializers, though. This thread tells when you can trust it and when you can't: http://stackoverflow.com/questions/14049777/why-global-variables-are-always-initialized-to-0-but-not-local-variables – Topological Sort May 06 '15 at 19:41
  • If you have to initialize a field different from 0, there is hardly a way around an explicit intializer. Ok, you might use assignments, but that is really nonsense, as that generates more code and you have the initialization at a different location in your code from the structure, If one uses a programming language, one should use its features and not try to program in C as one would in C++ (which includes language-support for the other way). – too honest for this site May 07 '15 at 13:31
0

It is ok to rely on permanent variables (non-auto,not dynamic) to be 0-initialized. However, the errors report a type error: next is an array of pointers, while you initialize it with a pointer to an array. If you really want a pointer to array, use struct node (*next)[]).

Therefore the second message which already includes the hint that you have a nested compound data type (array in struct). Remember that the initializer for every compound type requires to be enclosed in curly brackets.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • >Remember that the initializer for every compound type requires to be enclosed in curly brackets. I did not know this, thank you. May I also ask how I would have created an array of node*? – veta May 06 '15 at 20:16
  • @veta: Could you please define what you mean with "created"? Normally, you declare a type, the define it and then initialize it. This can be done by a single statement or seperately (but there are forbidden combinations) – too honest for this site May 07 '15 at 12:56