【发布时间】:2015-12-20 08:20:33
【问题描述】:
我正在学习 C 中的嵌套结构,我想做的是能够为我的结构的成员结构分配一个值。我很难弄清楚这一点,我不想强迫自己在结构的初始化中初始化成员结构。为什么我编译这段代码时总是报错?
main.c: In function 'main':
main.c:16:23: error: expected expression before '{' token
fooPerson.fullname = {"Foo", 'B', "Baz"};
#define LEN 20
struct names {
char first[LEN];
char middle;
char last[LEN];
};
struct person {
struct names fullname;
};
int main() {
struct person fooPerson;
fooPerson.fullname = {"Foo", 'B', "Baz"};
// NOT this: it works, but not the solution I'm asking for
// struct person fooPerson = {{"Foo", 'B', "Baz"}};
}
【问题讨论】:
-
你只能初始化一次。而且数组是不可赋值的。
-
对,我编辑了标题以反映您的评论。