这是我尝试初始化结构体 L 时使用的代码:
typedef struct {
int data[20];
int length;
} SqList;
SqList L;
L = {
{1,2,3,4,5},
5
};
最佳答案
您不是在初始化,而是在赋值,因为 L
的声明和赋值是代码中两个独立的语句。
写
SqList L = {
{1,2,3,4,5},
5
};
它应该可以工作。
您可以稍后覆盖这个初始值,但请注意,您需要分配一个 SqList 对象,并且分配必须发生在函数的上下文中:
int main() {
L = (SqList){ {1,2,3,4,5}, 5 };
}
关于c - 为什么错误: expected expression before '{' token happen when i try to initialize a struct variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52717345/