gpt4 book ai didi

带有结构的代码的编译错误

转载 作者:行者123 更新时间:2023-11-30 20:26:28 24 4
gpt4 key购买 nike

我尝试编译这个:

#define VAR_COUNT 5
typedef struct {
uint32_t value;
char name[10];
} variable;

variable cfg[VAR_COUNT];

cfg[0].value = 0;
cfg[0].name = "test";

但出现错误:

src/variables.c:26:7: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
cfg[0].value = 0;
src/variables.c:27:7: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
cfg[0].name = "test";

这对我来说真是意外的错误。我认为我的结构数组是错误的,但是使用以下代码:

typedef struct {
uint32_t value;
char name[10];
} variable;

variable cfg;

cfg.value = 0;
cfg.name = "test";

我也遇到同样的错误。

更新:我添加功能

void set (variable data, uint32_t value) {
data.value = value;
}

在其范围内,我可以使用结构元素而不会出现错误。

谢谢大家,问题已经解决了,在编写一些 C 代码之前,你应该保持完全清醒。

最佳答案

您不能分配给数组,但可以复制到它:

strcpy(cfg[0].name, "test");

关于带有结构的代码的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25860030/

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