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

时间:2019-10-19 17:52:01

标签: c++ arrays struct

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

#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的正确方法是什么,或者有人知道这样做的好方法吗?

2 个答案:

答案 0 :(得分:1)

我不知道是否可以按照您尝试的方式完成,但是将初始化分为两个工作。首先是子命令数组,然后是命令,并仅传递指针。

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

答案 1 :(得分:1)

如果在cmd_def_st结构中使用const CMD_DEF_ST* params;,则尝试使用结构数组初始化标量类型(指针)。 因此,这里实际上需要一个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 }
        }
    };