【发布时间】:2014-11-17 14:48:04
【问题描述】:
以下代码编译并运行:
#include <stdio.h>
void print(void* x)
{
printf("%d", *(int*)x);
}
int main()
{
print(&((struct { int x, y; }){ .x = 1, .y = 2 })); //outputs 1
return 0;
}
为什么编译器允许我获取右值的地址?这是定义的行为吗?
【问题讨论】:
-
这些是在 C99 中引入的指定初始化器,不是吗? gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
-
是的。 GCC 使用
x : 1。
标签: c gcc c99 undefined-behavior