无法解决'错误:在初始化程序周围丢失大括号'

时间:2014-02-16 16:22:20

标签: c struct compiler-errors anonymous-struct

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

error: missing braces around initializer [-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的新手,所以在那里道歉。

1 个答案:

答案 0 :(得分:1)

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)

E.g。

strcpy(carray, cp);