【发布时间】:2019-11-20 17:07:48
【问题描述】:
我想了解有关类型双关语和别名的更多信息。所以我使用GCC documentation 在选项-fstric-aliasing 提供的代码,如下所示:
union a_union{
int i;
double d;
};
int f(){
union a_union t;
t.d=3.0;
return t.i
}
/*what I add*/
int main(int argc, char *argv[])
{
printf("%d\n",f());
return 0;
}
我期望没有什么特别的,但我只得到 0。所以我测试了 t.d 的不同值,但没有变化。
我在 Debian 9 上使用 gcc 6.3 测试此代码,没有选项。
我也尝试使用选项-fno-strict-aliasing 相同的结果
如果有人可以解释一下,为什么我只得到 0 那就太好了
【问题讨论】:
-
您的代码中有
a_union和q_union。这实际上是您的代码,还是您在复制粘贴时出错了? -
哦,那是因为我用了两台电脑,一台是azerty键盘,另一台是qwerty,所以有时我会出错
标签: c union type-punning c-standard-library