2

I have the following struct:

typedef void (*HSM_State_Fcn)(void*, size_t);
typedef HSM_State_Fcn HSM_Dest_State_Fcn;
typedef uint8_t (*HSM_Guard_Fcn)(void*, size_t);
typedef void (*HSM_Action_Fcn)(void*, size_t);

typedef struct HSM_Transition_Tag
{
    HSM_Dest_State_Fcn dest_fcn;
    HSM_Guard_Fcn      guard_fcn;
    HSM_Action_Fcn     action_fcn;
}HSM_Transition_T;

And I have the following 2D array that I want every element to be NULL:

static HSM_Transition_T Transition_Table_Array_Test[3][3] = {NULL};

As you can see I tried to equal to {NULL} but I get the following warning:

warning: (near initialization for 'Transition_Table_Array_Test[0]') [-Wmissing-braces]

What would be the correct approach to initialize every element to NULL?

If I make it:

static HSM_Transition_T Transition_Table_Array_Test[3][3] = {{NULL}};

I get the same error.

2
  • 1
    Use two sets of braces for two-dimensional arrays: Stuff *stuff[3][3] = {{NULL}}; Commented May 19, 2015 at 15:47
  • 1
    FYI the struct is automatically initialized to zero because it is qualified static. You don't need to set it explicitly. Commented May 19, 2015 at 18:07

3 Answers 3

2
static HSM_Transition_T* Transition_Table_Array_Test[3][3]={{NULL}};

NULL should be used with pointers not structure.

If you want to initialize struct fields in 2d array of structs, then

static HSM_Transition_T Transition_Table_Array_Test[3][3]={{{NULL,NULL,NULL}}};
Sign up to request clarification or add additional context in comments.

6 Comments

I did this and still getting the same warning.
I just compiled simple c app in Netbeans IDE for static float* array[100][100]={{NULL}}; and it works fine.
You are right, I created one also and it compiles successfully but it is because you are using an array of pointers to float. My array is of pointer structs and it doesn't work the same, well I thing that because I still getting the warning.
I compiled your code and it works fine. Judging from your last comment you forgot to add ' * ' sign after HSM_Transition_T. It should be HSM_Transition_T* .
This worked, now it compiles fine. And I'm choosing this as the answer because it actually shows the third braced expression {{{NULL, NULL, NULL}}} needed to initialize each pointer within each struct in the array. According to the answer by Leushenko what I was trying to do with {{NULL}} is to initialize to NULL the structs instead of their contents.
|
2

The reason you're getting a warning is because NULL is a primitive, but the elements of your array are aggregates; they need to either be initialized with an aggregate variable (of the right type), or with yet a third braced expression (constructing an aggregate in-place).

The type of the array does not admit using NULL to initialize the elements, because structure values cannot be NULL, only pointers. Structures exist in-place, so it's not possible for them to be "absent" from the array (the elements of the array are the structures; if the array exists, so do they).

The compiler is complaining that braces are still missing, rather than telling you a structure value cannot be NULL, because it thinks you're trying to initialize the function pointers within the HSM_Transition_T to NULL (which is an operation that would make sense).

3 Comments

So basically you are saying that the expression {{NULL}} is telling the compiler that I want to initialize each element of the array, this is, each structure to NULL? And I need a third braced expression like {{{NULL, NULL, NULL}}} as suggested by Mac in his answer to indicate that what I'm trying to initialize as NULL are the elements within each struct in the array?
@m4l490n not quite. {{NULL}} tells the compiler that you're trying to initialize the struct elements to NULL, because the language is actually a bit permissive with braces and tries to interpret the correct course of action based on the type of initializer-list values; since NULL can't be applied to the struct it tries to read it in as an element. See C11 section 6.7.9 for a breakdown. Basically the compiler is warning you (note: it didn't give you an error) because the rules about inconsistent bracing are complicated.
In particular the warning is to point you at the fact that while it currently works correctly (because NULL is a zero value anyway), if you tried to add more initial values with the current bracing scheme, you'd probably end up doing something other than you expect.
1

You can write:

static HSM_Transition_T Transition_Table_Array_Test[3][3] = {
    {NULL, NULL, NULL},
    {NULL, NULL, NULL},
    {NULL, NULL, NULL}};

But NULL is mostly used with pointer and not struct.

You would have written (note the '*' before Transition_Table_Array_Test:

static HSM_Transition_T *Transition_Table_Array_Test[3][3] = {
    {NULL, NULL, NULL},
    {NULL, NULL, NULL},
    {NULL, NULL, NULL}};

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.