構造体の初期化

struct _str{
    int flag;
    unsigned char* ptr[4];
}str={0,NULL,NULL,NULL,NULL};

とすると、

odawara@looxp ~/prog> gcc -Wall test.c 
test.c:5: 警告: 初期化子のまわりのブレースを欠いています
test.c:5: 警告: (near initialization for ‘str.ptr’)

と言われる。日本語だと意味がわからん。set LANG C。

test.c:5: warning: missing braces around initializer
test.c:5: warning: (near initialization for 'str.ptr')

ああ、そうか配列だから、

struct _str{
    int flag;
    unsigned char* ptr[4];
}str={0,{NULL,NULL,NULL,NULL}};

が正しい。よく忘れるなぁ、これ。

新版 明解C言語 入門編

新版 明解C言語 入門編