Sun编译器提供 " b.c",第6行:警告:未命名的工会成员 " b.c",第10行:警告:未命名的工会成员 " b.c",第11行:零大小的struct / union cc:acomp因bc而失败 gcc -Wall -ansi -pedantic bc给出 bc:在函数`main'': bc:6:警告:ANSI C禁止成员声明没有成员 bc:6:警告:未命名的struct / union定义没有实例 bc:10:警告:ANSI C禁止没有成员的成员声明 bc:10:警告:未命名的struct / union,它不定义实例 bc:11:警告:工会没有会员 bc:11:警告:未使用的变量`un'' bc:12:警告:控制到达无效功能的结束 我建议你把你的编译器警告 升级。如果您仍然没有收到警告,那么转换到新编译器的时间是 。 Annonymous Union的主要用途是限制某些名称的范围 空间。如果你要定义Anno。工会,你必须自己照顾 变量的范围。 Hi all,I wanted to have a union which has two structures in it.So i did this,union {struct{int a;int b;};struct{float c;};}un;I did not want to name the two inner structures, because i wanted toaccess the members like this,<union name>.<member name>instead of<union name>.<structure name>.<member name>My question is, Is this valid C?I also observed that if i have a union like this ,union {struct{int a;int b;};struct{float a;float b;};}un;Compiler doesn''t give me any error.If i say, ''un.a'' how will it know whether i''m using float a or inta ?Thanks for your time,Yugi. 解决方案No. The compiler cannot read your mind and realizethat when you write <union name>.<member name>you really mean <union name>.<some structure>.<member name>It won''t. Hence your definition is useless even if itdoesn''t cause your compiler to complain. For theprogrammeint main(void) {union {struct{int a;int b;};struct{float a;float b;};}un;}Sun compiler gives"b.c", line 6: warning: unnamed union member"b.c", line 10: warning: unnamed union member"b.c", line 11: zero-sized struct/unioncc: acomp failed for b.cgcc -Wall -ansi -pedantic b.c givesb.c: In function `main'':b.c:6: warning: ANSI C forbids member declarations with no membersb.c:6: warning: unnamed struct/union that defines no instancesb.c:10: warning: ANSI C forbids member declarations with no membersb.c:10: warning: unnamed struct/union that defines no instancesb.c:11: warning: union has no membersb.c:11: warning: unused variable `un''b.c:12: warning: control reaches end of non-void functionI would suggest that you turn your compiler''s warninglevels up. If you still don''t get a warning then it''s timeto switch to a new compiler.No. The compiler cannot read your mind and realizethat when you write <union name>.<member name>you really mean <union name>.<some structure>.<member name>It won''t. Hence your definition is useless even if itdoesn''t cause your compiler to complain. For theprogrammeint main(void) { union { struct{ int a; int b; }; struct{ float a; float b; }; }un;}Sun compiler gives"b.c", line 6: warning: unnamed union member"b.c", line 10: warning: unnamed union member"b.c", line 11: zero-sized struct/unioncc: acomp failed for b.cgcc -Wall -ansi -pedantic b.c givesb.c: In function `main'':b.c:6: warning: ANSI C forbids member declarations with no membersb.c:6: warning: unnamed struct/union that defines no instancesb.c:10: warning: ANSI C forbids member declarations with no membersb.c:10: warning: unnamed struct/union that defines no instancesb.c:11: warning: union has no membersb.c:11: warning: unused variable `un''b.c:12: warning: control reaches end of non-void functionI would suggest that you turn your compiler''s warninglevels up. If you still don''t get a warning then it''s timeto switch to a new compiler.The main usage of Annonymous Union is to limit scope of some namespace. If you are defining Anno. union, you have to take care scope ofvariable yourself. 这篇关于这合法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 07:02