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:
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.
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}}};
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).
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}};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.