我是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;
}
Run Code Online (Sandbox Code Playgroud)
不是直接的,但C99有复合文字
bill = (Person){120,34};
Run Code Online (Sandbox Code Playgroud)
你甚至可以通过使用指定的初始化程序来做更可读的事情
bill = (Person){ .height = 120, .age = 34, };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
617 次 |
| 最近记录: |