gpt4 book ai didi

c - C 上的初始化结构问题

转载 作者:行者123 更新时间:2023-11-30 15:55:39 25 4
gpt4 key购买 nike

我在编译过程中遇到此错误:

"c:\command_line.h(17): error C2143: syntax error : missing ';' before '*' Note: C++ does not support default-int command_line.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int"

这是我的代码:

命令行.h

typedef struct symbol
{
char* sym_type;
unsigned short address;
}symbol;


typedef struct symbol_map
{
char** p_arr_keys;
symbol* p_arr_values;
int item_count;
int array_mode;
int copy_keys;
}symbol_map;

typedef struct params
{
int data_counter;
int code_counter;
int line_counter;
int command_len;
int error_counter;
int warning_counter;
symbol_map* p_symbol_map; // (This is line 17- from the error msg)
char* p_last_symbol
}params;

main.c

params config;
config.code_counter = 0;
config.data_counter = 0;
config.line_counter = 0;
config.command_len = 0;
config.command_first_char = EMPTY;
config.error_counter = 0;
config.warning_counter = 0;
config.p_last_symbol = NULL;
config.p_symbol_map = {NULL}; // (This is line 17- from the error msg)

知道这个初始化有什么问题吗?

 config.p_symbol_map = {NULL};

最佳答案

您使用的构造config.p_symbol_map = {NULL}是静态初始化,并且仅允许在变量声明中使用。如果您想将 NULL 分配给 p_symbol_map,您可以简单地 config.p_symbol_map = NULL

应用于 symbol 结构的静态初始化的有效情况如下所示:

symbol sym = {
NULL,
0x42
};

更新:

顺便说一句,在结构定义中的 char* p_last_symbol 后面缺少一个分号。


typedef struct params
{
int data_counter;
int
int line_counter;
int command_len;
int error_counter;
int warning_counter;
symbol_map* p_symbol_map; // (This is line 17- from the error msg)
char* p_last_symbol <----- need to add ; here
}params;

关于c - C 上的初始化结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903738/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com