我假设像这样的演员是合法的(其中foo是指向void的指针):
struct on_off { unsigned light : 1; unsigned toaster : 1; int count; /* 4 bytes */ unsigned ac : 4; unsigned : 4; unsigned clock : 1; unsigned : 0; unsigned flag : 1; }; ((on_off) foo).count = 3;
但我想知道结构是否未定义是否这样的东西是合法的:
((struct { unsigned light : 1; unsigned toaster : 1; int count; /* 4 bytes */ unsigned ac : 4; unsigned : 4; unsigned clock : 1; unsigned : 0; unsigned flag : 1; }) foo).count = 3;
……或者沿着这些方向的东西。
谢谢!
是的,C允许转换为匿名结构。 这是一个快速演示:
struct xxx { int a; }; ... // Declare a "real"struct, and assign its field struct xxx x; xa = 123; // Cast a pointer of 'x' to void* void *y = &x; // Access a field of 'x' by casting to an anonymous struct int b = ((struct {int a;}*)y)->a; printf("%dn", b); // Prints 123
在ideone上演示 。
以上就是c/c++开发分享匿名结构声明符在Ansi C中是合法的吗?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/559720.html