该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
嗯嗯,翻了C99标准大概如下:
6.5 Expressions
7 An object shall have its stored value accessed only by an lvalue expression that has one of the following types:73)
— a type compatible with the effective type of the object,
— a qualified version of a type compatible with the effective type of the object,
— a type that is the signed or unsigned type corresponding to the effective type of the
object,
— a type that is the signed or unsigned type corresponding to a qualified version of the
effective type of the object,
— an aggregate or union type that includes one of the aforementioned types among its
members (including, recursively, a member of a subaggregate or contained union), or
— a character type.
Undefined behavior也有:
— An object has its stored value accessed other than by an lvalue of an allowable type
(6.5).
不过我还是对object所指存疑,即使以blog所给例子来看,稍微修改如下:
#include
int a;
int f(float *b)
{
a = 1;
*(int*)(b) = 0;
return a;
}
int g(float *b)
{
a = 1
*b = 0;
return a;
}
int main()
{
printf("%d\n", f((float*)&a));
printf("%d\n", g((float*)&a));
return 0;
}
gcc -O3结果是
0
1
关于这条我一般都是在别名的时候才会注意到,这种类型转换还真没注意= =;等我在去理解下……