无法通过 'error: missing braces around initializer'

标签 c struct compiler-errors anonymous-struct

执行下面的匿名结构时出现以下错误:

错误:初始化程序周围缺少大括号 [-Werror=missing-braces]

如果我将“message”替换为“lalala”等静态字符​​串,效果很好。

typedef struct {
    /* public: */
    char message[255];
} Note;

static uint16_t local_size = 0;
static Note *notes;


Note *add_local_note(const char *_message) {
    //char bla[255] = "hot hot hot";

    notes[(++local_size)-1] = (Note) {
        .message = _message
    };

    return notes;
}

有什么想法吗?是的,我是 C 语言新手,所以很抱歉。

最佳答案

C不允许这样

char *cp = "sample";    
char carray[10] = cp;//NG, Type is different rather than that is not able to use the variables.

char carray[10] = "sample";// or { "test" };//OK

使用strcpy(或strncpy)

例如

strcpy(carray, cp);

关于无法通过 'error: missing braces around initializer',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21813907/

相关文章:

c - sdl中对象自动移动

scala - 以编程方式从 Storm Bolt 杀死拓扑时出现编译错误 : object and package with same name

c - 为什么这个动态字符数组在 C 中没有得到正确的输入?

c - scanf: "%[^\n]"跳过第二个输入但 "%[^\n]"没有。为什么?

java - 使用 Amorino 从 Arduino 向 Android 发送和接收数据结构(反之亦然)

c - dll 的结构参数被损坏

不透明结构的 C typedef 编码风格

compiler-errors - 到底应该使用什么命令行来从 <Target>RegisterInfo.td 文件中生成所有记录?

compiler-errors - 药剂: Why do I get “undefined function” error?

c - SNMP如何获取代理并为其设置值