我想创建一个结构类型的数组,但它没有按顺序定义,例如,我可以随机定义array[3] = mystruct_t; array[5] = mystruct_t(3并且5在运行时通过用户输入定义).我试过了:
它是"理想的",但不能保持空指针:
static struct mystruct_t foo_inputs[SIZE];
Run Code Online (Sandbox Code Playgroud)
所以,我尝试过:
static struct mystruct_t *foo_inputs;
Run Code Online (Sandbox Code Playgroud)
然后:
foo_inputs[x] = NULL;
Run Code Online (Sandbox Code Playgroud)
但我得到一个警告:
error: incompatible types when assigning to type ‘struct mystruct_t’ from type ‘void *’
Run Code Online (Sandbox Code Playgroud)
实现它的想法?
将两者结合起来.在第一个中,您不能(不应该)分配NULL给非指针.在第二个,你有一个未初始化的指针.你想要的是一个指针数组:
static struct mystruct_t *foo_inputs[SIZE];
foo_inputs[x] = NULL;
Run Code Online (Sandbox Code Playgroud)