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
:
我有下面的2D数组我想让每个元素都是空的:
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:
如您所见,我试图等于{NULL},但我得到以下警告:
warning: (near initialization for 'Transition_Table_Array_Test[0]') [-Wmissing-braces]
警告:(对“Transition_Table_Array_Test[0]”进行近初始化)
What would be the correct approach to initialize every element to NULL
?
将每个元素初始化为NULL的正确方法是什么?
If I make it:
如果我让它:
static HSM_Transition_T Transition_Table_Array_Test[3][3] = {{NULL}};
I get the same error.
我得到了同样的错误。
3 个解决方案
#1
2
static HSM_Transition_T* Transition_Table_Array_Test[3][3]={{NULL}};
NULL should be used with pointers not structure.
NULL应该用于指针而不是结构。
If you want to initialize struct fields in 2d array of structs, then
如果要在结构体的2d数组中初始化结构体字段,则
static HSM_Transition_T Transition_Table_Array_Test[3][3]={{{NULL,NULL,NULL}}};
#2
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).
得到警告的原因是NULL是一个原语,但是数组的元素是聚合的;它们需要使用聚合变量(正确的类型)初始化,或者使用第三个支撑表达式(在适当的位置构造聚合)初始化。
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).
数组的类型不允许使用NULL来初始化元素,因为结构值不能为NULL,只能是指针。结构存在于适当的位置,所以它们不可能从数组中“缺席”(数组的元素是结构;如果数组存在,那么它们也存在)。
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).
编译器在抱怨大括号仍然丢失,而不是告诉您结构值不能为NULL,因为它认为您正在尝试将HSM_Transition_T中的函数指针初始化为NULL(这是一个有意义的操作)。
#3
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.
但是NULL主要用于指针,而不是struct。
You would have written (note the '*' before Transition_Table_Array_Test
:
您应该已经编写(请注意Transition_Table_Array_Test之前的“*”:
static HSM_Transition_T *Transition_Table_Array_Test[3][3] = {
{NULL, NULL, NULL},
{NULL, NULL, NULL},
{NULL, NULL, NULL}};
#1
2
static HSM_Transition_T* Transition_Table_Array_Test[3][3]={{NULL}};
NULL should be used with pointers not structure.
NULL应该用于指针而不是结构。
If you want to initialize struct fields in 2d array of structs, then
如果要在结构体的2d数组中初始化结构体字段,则
static HSM_Transition_T Transition_Table_Array_Test[3][3]={{{NULL,NULL,NULL}}};
#2
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).
得到警告的原因是NULL是一个原语,但是数组的元素是聚合的;它们需要使用聚合变量(正确的类型)初始化,或者使用第三个支撑表达式(在适当的位置构造聚合)初始化。
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).
数组的类型不允许使用NULL来初始化元素,因为结构值不能为NULL,只能是指针。结构存在于适当的位置,所以它们不可能从数组中“缺席”(数组的元素是结构;如果数组存在,那么它们也存在)。
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).
编译器在抱怨大括号仍然丢失,而不是告诉您结构值不能为NULL,因为它认为您正在尝试将HSM_Transition_T中的函数指针初始化为NULL(这是一个有意义的操作)。
#3
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.
但是NULL主要用于指针,而不是struct。
You would have written (note the '*' before Transition_Table_Array_Test
:
您应该已经编写(请注意Transition_Table_Array_Test之前的“*”:
static HSM_Transition_T *Transition_Table_Array_Test[3][3] = {
{NULL, NULL, NULL},
{NULL, NULL, NULL},
{NULL, NULL, NULL}};