0x00001f62
0x00001f63
0x00001f64
0x00001f65
0x00001f66
0x00001ef0
0x00001ef1
0x00001ef3
0x00001ef4
0x00001ef5
0x00001ef6
0x00001ef9
0x00001efc
0x00001eff
0x00001f04
0x00001f0a
0x00001f10
0x00001f15
0x00001f1c
0x00001f1f
0x00001f22
0x00001f29
0x00001f2c
0x00001f34
0x00001f37
0x00001f3a
0x00001f3d
0x00001f42
0x00001f45
0x00001f48
0x00001f4b
0x00001f4f
0x00001f52
0x00001f57
0x00001f5a
0x00001f5d
0x00001f5f
0x00001f62
0x00001f63
0x00001f64
0x00001f65
0x00001f66
Q5: 为什么形如2["hello"]这样的表达式是什么意思?
[cpp]
#include
#define PRINT_D(intValue) printf(#intValue" is %d\n", (intValue));
#define PRINT_CH(charValue) printf(#charValue" is %c\n", (charValue));
int main(int argc, char **argv)
{
char ch = 2["hello"];
PRINT_CH(ch)
return 0;
}
#include
#define PRINT_D(intValue) printf(#intValue" is %d\n", (intValue));
#define PRINT_CH(charValue) printf(#charValue" is %c\n", (charValue));
int main(int argc, char **argv)
{
char ch = 2["hello"];
PRINT_CH(ch)
return 0;
}
A: 2["hello"]等同于"hello"[2], 又等同于 char *str = "hello"; str[2] . 但是,为什么?
因为编译器对于数组访问根本就采用指针加减的计算方法,即2["hello"] == *(2 + "hello"), "hello"是个字符指针, *(2 + "hello") == *("hello" + 2), 所以结论也就出来了。
Q6: 为什么一个整形数据用%f格式输出,结果改变了?
[cpp]
#include
#define PRINT_D(intValue) printf(#intValue" is %d\n", (intValue));
#define PRINT_F(floatValue) printf(#floatValue" is %f\n", (floatValue));
int main()
{
int d = 2;
PRINT_F(d)
return 0;
}
#include
#define PRINT_D(intValue) printf(#intValue" is %d\n", (intValue));
#define PRINT_F(floatValue) printf(#floatValue" is %f\n", (floatValue));
int main()
{
int d = 2;
PRINT_F(d)
return 0;
}
A: 原因在于printf分析%f格式时是从对应地址取出sizeof(float)个字节数据然后当成float格式输出,所以,一般情况下,输出都是错误的。
对于float内部的格式,IEEE 754标准写的很清楚,如果出现类似的问题,可以使用如下结构来简单地验证:
[cpp]
// IEEE 754 single floating number's format(intel little-endian mode)
typedef union
{
// float value
float f;
// intel bits mode that stands for float value
struct
{
unsigned int dot : 23; // low bits
unsigned int exp : 8; // middle bits
unsigned int sign : 1; // highest bit
}float_bit;
}float_value;
// IEEE 754 single floating number's format(intel little-endian mode)
typedef union
{
// float value
float f;
// intel bits mode that stands for float value
struct
{
unsigned int dot : 23; // low bits