[英]How to initialize to NULL a 2D array of pointer structs in C?
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数组,我希望每个元素都为
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: 如您所见,我尝试等于
{NULL}
但收到以下警告:
warning: (near initialization for 'Transition_Table_Array_Test[0]') [-Wmissing-braces]
警告:(在“ Transition_Table_Array_Test [0]”的初始化附近)[-Wmissing-括号]
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. 我犯了同样的错误。
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 如果要在二维结构数组中初始化结构字段,则
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; 您收到警告的原因是因为
NULL
是基元,但是数组的元素是集合; 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. 数组的类型不允许使用
NULL
来初始化元素,因为结构值不能为NULL
,只能是指针。 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). 编译器抱怨括号仍然下落不明,而不是告诉你一个结构值不能为
NULL
,因为它认为你想在内部的函数指针初始化HSM_Transition_T
以NULL
(这是一个操作将使意义)。
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}};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.