[英]How to initialise array of structs in another struct?
我有以下代碼會產生編譯錯誤。 它適用於用 C++ 編寫的 STM32 微控制器的命令解析器。 基本上我有一個帶有字符串的CMD_DEF_ST
結構和一個 function 指針來處理該字符串。 我在結構中添加了另一個指向另一個CMD_DEF_ST
數組的指針,以便能夠處理命令參數。 不幸的是,我無法正確初始化。 我不想在運行時初始化它,因為它都必須將 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;
}
我在這里查看了gcc 警告:在標量初始化器周圍大括號,在這里初始化指向 structs 的指針數組。 它給了我一些嘗試的想法,但我無法弄清楚如何讓它適用於我自己的代碼。
我嘗試將結構定義更改為:
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
};
和
// **************************
// 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
}
};
但我仍然遇到編譯錯誤。
有人可以告訴我初始化cmd2
的正確方法是什么,或者有人知道這樣做的好方法嗎?
我不知道它是否可以按照您嘗試的方式完成,但是將初始化分為兩個工作。 首先是子命令數組,然后是命令,只是傳入指針。
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};
如果在 cmd_def_st 結構中使用const CMD_DEF_ST* params;
您正在嘗試使用結構數組初始化標量類型(指針)。 所以在這里你實際上需要一個 CMD_DEF_ST 類型的地址(就像在超級答案中建議的那樣)。
在第二種情況下,您嘗試獲取右值地址,一個臨時的 object 在當前語句之后將不復存在,因此您收到錯誤消息。
如果您對兩步初始化不滿意,我可能會建議為 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.