c++ - 如何在另一个结构中初始化结构数组?

标签 c++ arrays struct

我有以下产生编译错误的代码。它用于用 C++ 编写的 STM32 微 Controller 的命令解析器。基本上我有一个 CMD_DEF_ST 结构,其中包含一个字符串和一个函数指针来处理该字符串。我在结构中添加了一个指向另一个 CMD_DEF_ST 数组的指针,以便能够处理命令参数。不幸的是,我无法正确初始化。我不想在运行时初始化它,因为它都必须进入常量闪存。

#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 warning: braces around scalar initializer在这里 initializing array of pointers to 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 的正确方法是什么,或者有人知道这样做的好方法吗?

最佳答案

如果在 cmd_def_st 结构中使用 const CMD_DEF_ST* params;,您正在尝试使用结构数组初始化标量类型(指针)。 所以在这里你实际上需要一个 CMD_DEF_ST 类型的地址(就像 super 答案中建议的那样)。

在你的第二种情况下,你试图获取一个右值地址,一个临时对象,它将在当前语句之后不复存在,因此你收到一个错误。

如果您对两步初始化不满意,我可能建议为 cmds 和 subcmds 创建结构,以便为 cmd_def_st 中的参数数组提供类型完整性:

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 }
        }
    };

关于c++ - 如何在另一个结构中初始化结构数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58466462/

相关文章:

C++11 的 shared_ptr 赋值

python - 由于数组大小的微小差异,创建 numpy.zeros 的时间差异很大

c - 为结构中的变量分配内存的最佳方法是什么?

JavaScript 2D 数组为空时推送

c++ - Qt、c++ QML 和 HWND

c++ - Linux:作为共享对象 API 的 C++ 抽象类

c - 无法为结构分配值

c - 如何在循环中创建节点并将它们插入链表

c# - 从 C 函数返回结构,C# 中的等价物是什么?

c++ - Kate 之类的程序中使用的 Kde 栏的小部件名称是什么? (内图)