我是 C 的新手,正在尝试使用结构。在我创建一个结构后,是否可以用花括号重新分配它?
typedef struct {
int height;
int age;
} Person;
int main (void)
{
Person bill = {100,35};
bill = {120,34}; // error: expected expression before ‘{’ token
bill = Person {120,34}; // error: expected expression before ‘Person’
return 0;
}
最佳答案
不是直接的,但是 C99 有复合文字
bill = (Person){120,34};
你甚至可以通过使用指定的初始值设定项来使事情更具可读性
bill = (Person){ .height = 120, .age = 34, };
关于c - 在 C 中重新分配结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10298070/