[英]How to initialise array of structs in another struct?
I have the following code that produces a compile error.我有以下代码会产生编译错误。 It is for a command parser for a STM32 micro-controller written in C++.
它适用于用 C++ 编写的 STM32 微控制器的命令解析器。 Basically I have a
CMD_DEF_ST
struct with a string and a function pointer to handle that string.基本上我有一个带有字符串的
CMD_DEF_ST
结构和一个 function 指针来处理该字符串。 I added a further pointer to another CMD_DEF_ST
array in the struct, to be able to handle command parameters.我在结构中添加了另一个指向另一个
CMD_DEF_ST
数组的指针,以便能够处理命令参数。 I cannot get the initialisation right unfortunately.不幸的是,我无法正确初始化。 I do not want to initialize it at runtime, as it all has to go into const FLASH memory.
我不想在运行时初始化它,因为它都必须将 go 转换为 const FLASH memory。
#include <stdio.h>
// **************************
// define command structure
// **************************
// struct forward declaration
typedef struct cmd_def_st CMD_DEF_ST;
// typedef for command handler function
typedef void (*CMD_FUNC_HANDLER)(const CMD_DEF_ST* cmddef);
struct cmd_def_st
{
const char* cmdstr; // command string
const CMD_FUNC_HANDLER func; // pointer to handler
const CMD_DEF_ST* params; // pointer to sub parameters
};
// **************************
// declare some commands
// **************************
const CMD_DEF_ST cmd = {"swrst" , [] (const CMD_DEF_ST* cmd) { printf("swrst\n"); },
NULL};
// **************************
// produces:
// error: braces around scalar initializer for type 'const CMD_DEF_ST*
// {aka const cmd_def_st*}'
// **************************
const CMD_DEF_ST cmd2 = { "echo" , [] (const CMD_DEF_ST* cmd) { printf("Error\n"); },
{
{"on" , [] (const CMD_DEF_ST* cmd) { printf("Echo on\n"); }, NULL },
{"off" , [] (const CMD_DEF_ST* cmd) { printf("Echo off\n"); }, NULL },
NULL
}
};
int main()
{
cmd.func(&cmd); // prints "swrst"
return 0;
}
I had a look here gcc warning: braces around scalar initializer and here initializing array of pointers to structs .我在这里查看了gcc 警告:在标量初始化器周围大括号,在这里初始化指向 structs 的指针数组。 It gave me a few ideas to try, but I could not quite figure out how to make it work for my own code.
它给了我一些尝试的想法,但我无法弄清楚如何让它适用于我自己的代码。
I tried changing the struct definition to:我尝试将结构定义更改为:
struct cmd_def_st
{
const char* cmdstr; // command string
const CMD_FUNC_HANDLER func; // pointer to handler
const CMD_DEF_ST** params; // pointer to sub parameters
};
with和
// **************************
// produces:
// error: taking address of temporary [-fpermissive]|
// error: taking address of temporary [-fpermissive]|
// error: taking address of temporary array|
// **************************
const CMD_DEF_ST cmd2 = { "echo" , [] (const CMD_DEF_ST* cmd) { printf("Error\n"); },
(const CMD_DEF_ST* []){
&(const CMD_DEF_ST){"on" , [] (const CMD_DEF_ST* cmd) { printf("Echo on\n"); }, NULL },
&(const CMD_DEF_ST){"off" , [] (const CMD_DEF_ST* cmd) { printf("Echo off\n"); }, NULL },
NULL
}
};
but I still got a compile error.但我仍然遇到编译错误。
Can someone tell me what is the correct way to initialize cmd2
, or does someone know of a good way to do this?有人可以告诉我初始化
cmd2
的正确方法是什么,或者有人知道这样做的好方法吗?
I don't know if it can be done in the way you are trying, but splitting the initialization in two works.我不知道它是否可以按照您尝试的方式完成,但是将初始化分为两个工作。 First the array of sub-commands followed by the command and just passing in the pointer.
首先是子命令数组,然后是命令,只是传入指针。
const CMD_DEF_ST subcmd1[] = {{"on" , [] (const CMD_DEF_ST* cmd) { printf("Echo on\n"); }, NULL }, {"off" , [] (const CMD_DEF_ST* cmd) { printf("Echo off\n"); }, NULL }};
const CMD_DEF_ST cmd2 = { "echo" , [] (const CMD_DEF_ST* cmd) { printf("Error\n"); }, subcmd1};
if in the cmd_def_st struct you use const CMD_DEF_ST* params;
如果在 cmd_def_st 结构中使用
const CMD_DEF_ST* params;
you are trying to initialize a scalar type (pointer) with array of structs.您正在尝试使用结构数组初始化标量类型(指针)。 So here you actually need an address of CMD_DEF_ST type (like it is suggested in the super's answer).
所以在这里你实际上需要一个 CMD_DEF_ST 类型的地址(就像在超级答案中建议的那样)。
In your second case you are trying to take an address of rvalue, a temporary object that will cease to exist after the current statement, so you are receiving an error.在第二种情况下,您尝试获取右值地址,一个临时的 object 在当前语句之后将不复存在,因此您收到错误消息。
if you are not happy with that two-step initialization I might suggest making structs both for cmds and subcmds to provide type completeness for the params array in the cmd_def_st
:如果您对两步初始化不满意,我可能会建议为 cmds 和 subcmds 创建结构,以便为
cmd_def_st
中的 params 数组提供类型完整性:
struct subcmd_def_st
{
const char* cmdstr; // command string
const CMD_FUNC_HANDLER func; // pointer to handler
// const SUBCMD_DEF_ST params; // assuming no sub-subcmds
};
struct cmd_def_st
{
const char* cmdstr; // command string
const CMD_FUNC_HANDLER func; // pointer to handler
const subcmd_def_st params[]; // pointer to sub parameters
};
const CMD_DEF_ST cmd2 =
{
"echo" ,
[] (const CMD_DEF_ST* cmd) { printf("Error\n"); },
{
{ "on" , [] (const CMD_DEF_ST* cmd) { printf("Echo on\n"); } },
{ "off" , [] (const CMD_DEF_ST* cmd) { printf("Echo off\n"); } },
{ NULL }
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.