参见英文答案 >
Can a union be initialized in the declaration?3
如果它是一个结构,那么它可以完成
如果它是一个结构,那么它可以完成
*p = {var1,var2..};
但似乎这并不适用于联盟:
union Ptrlist { Ptrlist *next; State *s; }; Ptrlist *l; l = allocate_space(); *l = {NULL};
只得到:
expected expression before ‘{’ token
解决方法
在C99中,您可以使用d
esignated union initializer:
union { char birthday[9]; int age; float weight; } people = { .age = 14 };
在C,unions can have constructors.
在C89中,你必须明确地做.
typedef union { int x; float y; void *z; } thing_t; thing_t foo; foo.x = 2;
顺便说一句,你是否知道在C联合会,all the members share the same memory space?
int main () { thing_t foo; printf("x: %p y: %p z: %p\n",&foo.x,&foo.y,&foo.z ); return 0; }
输出:
x: 0xbfbefebc y: 0xbfbefebc z: 0xbfbefebc
原文地址:https://www.jb51.cc/c/114465.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。