重新分配结构用C [英] reassign struct in C
本文介绍了重新分配结构用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新的C和结构试验。我创建了一个结构之后,才可能有大括号重新分配呢?
typedef结构{
INT高度;
INT年龄;
}人;INT主要(无效)
{
人比尔= {100,35};
比尔= {120,34}; //错误:在{令牌预计前pression
比尔= {个人} 120,34; //错误:人之前的预期前pression 返回0;
}
解决方案
不是直接的,但C99具有该复合文字
=账单(人){} 120,34;
你甚至可以做得更有可读性使用指定的初始化像
=账单(人){.height = 120。年龄= 34,};
I'm new to C and experimenting with structs. After I've created a struct, is it possible to reassign it with curly brackets?
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;
}
解决方案
Not directly, but C99 has compound literals for that
bill = (Person){120,34};
you could even do things more readable by using designated initializers like
bill = (Person){ .height = 120, .age = 34, };
这篇关于重新分配结构用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文