gpt4 book ai didi

c - 在 C 中重新分配结构

转载 作者:太空狗 更新时间:2023-10-29 15:59:19 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com