我正在尝试打印一个int作为原始数据,仅仅是为了好奇,通过将变量指针强制转换为bool,并递增指针“sizeof(int)* 8”次,但我没有得到预期的行为。你能解释一下为什么吗?我错过了什么?
这是我的代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int integer = 0;
int *puntInt = 0;
bool *puntBool = 0;
puntInt = &integer;
for (int j = 0; j < 100; j++)
{
integer = j;
puntBool = (bool*) puntInt;
printf("%02d => ", integer);
for(unsigned int i = 0; i < (sizeof(int) * 8); i++)
{
printf("%d", (*puntBool ? 1 : 0));
puntBool++;
}
printf("\n");
}
getchar();
return 0;
}
这是我的输出:
00 => 00000000100011111100111111000000
01 => 10001000100011111100111111000000
02 => 10001000100011111100111111000000
03 => 10001000100011111100111111000000
04 => 10001000100011111100111111000000
05 => 10001000100011111100111111000000
06 => 10001000100011111100111111000000
07 => 10001000100011111100111111000000
08 => 10001000100011111100111111000000
09 => 10001000100011111100111111000000
10 => 10001000100011111100111111000000
11 => 10001000100011111100111111000000
12 => 10001000100011111100111111000000
13 => 10001000100011111100111111000000
14 => 10001000100011111100111111000000
15 => 10001000100011111100111111000000
16 => 10001000100011111100111111000000
17 => 10001000100011111100111111000000
18 => 10001000100011111100111111000000
19 => 10001000100011111100111111000000
20 => 10001000100011111100111111000000
21 => 10001000100011111100111111000000
22 => 10001000100011111100111111000000
23 => 10001000100011111100111111000000
24 => 10001000100011111100111111000000
25 => 10001000100011111100111111000000
26 => 10001000100011111100111111000000
27 => 10001000100011111100111111000000
28 => 10001000100011111100111111000000
29 => 10001000100011111100111111000000
30 => 10001000100011111100111111000000
31 => 10001000100011111100111111000000
32 => 10001000100011111100111111000000
33 => 10001000100011111100111111000000
34 => 10001000100011111100111111000000
35 => 10001000100011111100111111000000
36 => 10001000100011111100111111000000
37 => 10001000100011111100111111000000
38 => 10001000100011111100111111000000
39 => 10001000100011111100111111000000
40 => 10001000100011111100111111000000
41 => 10001000100011111100111111000000
42 => 10001000100011111100111111000000
43 => 10001000100011111100111111000000
44 => 10001000100011111100111111000000
45 => 10001000100011111100111111000000
46 => 10001000100011111100111111000000
47 => 10001000100011111100111111000000
48 => 10001000100011111100111111000000
49 => 10001000100011111100111111000000
50 => 10001000100011111100111111000000
51 => 10001000100011111100111111000000
52 => 10001000100011111100111111000000
53 => 10001000100011111100111111000000
54 => 10001000100011111100111111000000
55 => 10001000100011111100111111000000
56 => 10001000100011111100111111000000
57 => 10001000100011111100111111000000
58 => 10001000100011111100111111000000
59 => 10001000100011111100111111000000
60 => 10001000100011111100111111000000
61 => 10001000100011111100111111000000
62 => 10001000100011111100111111000000
63 => 10001000100011111100111111000000
64 => 10001000100011111100111111000000
65 => 10001000100011111100111111000000
66 => 10001000100011111100111111000000
67 => 10001000100011111100111111000000
68 => 10001000100011111100111111000000
69 => 10001000100011111100111111000000
70 => 10001000100011111100111111000000
71 => 10001000100011111100111111000000
72 => 10001000100011111100111111000000
73 => 10001000100011111100111111000000
74 => 10001000100011111100111111000000
75 => 10001000100011111100111111000000
76 => 10001000100011111100111111000000
77 => 10001000100011111100111111000000
78 => 10001000100011111100111111000000
79 => 10001000100011111100111111000000
80 => 10001000100011111100111111000000
81 => 10001000100011111100111111000000
82 => 10001000100011111100111111000000
83 => 10001000100011111100111111000000
84 => 10001000100011111100111111000000
85 => 10001000100011111100111111000000
86 => 10001000100011111100111111000000
87 => 10001000100011111100111111000000
88 => 10001000100011111100111111000000
89 => 10001000100011111100111111000000
90 => 10001000100011111100111111000000
91 => 10001000100011111100111111000000
92 => 10001000100011111100111111000000
93 => 10001000100011111100111111000000
94 => 10001000100011111100111111000000
95 => 10001000100011111100111111000000
96 => 10001000100011111100111111000000
97 => 10001000100011111100111111000000
98 => 10001000100011111100111111000000
99 => 10001000100011111100111111000000
答案 0 :(得分:1)
bool
不仅仅是1位类型。它的大小是1个字节。
答案 1 :(得分:0)
char是最小的可寻址单位,你不能低于C / C ++ 打印puntBool持有的值:
for(unsigned int i = 0; i < (sizeof(int) * 8); i++)
{
//printf("%d", (*puntBool ? 1 : 0));
printf("%p\n",puntBool++);
}
给了我:
0xbfbb7d80
0xbfbb7d81
0xbfbb7d82
0xbfbb7d83
0xbfbb7d84
0xbfbb7d85
0xbfbb7d86
0xbfbb7d87
0xbfbb7d88
0xbfbb7d89
0xbfbb7d8a
0xbfbb7d8b
因此,bool*
基本上充当char*
,这就是为什么您的结果不符合预期。