#include <stdio.h>
union A{
int i;
char c[2];
};
int main(){
union A t;
t.c[0] = 10;
t.c[1] = 1;
printf("%d", t.i);
return 0;
}
这个程序输出是什么?
(gdb) l
8
9 int main(int argc, const char *argv[])
10 {
11 union A t;
12
13 t.c[0] = 10;
14 t.c[1] = 1;
15
16 printf("%d\n", t.a);
17 return 0;
(gdb) b 13
Note: breakpoint 2 also set at pc 0x100000f07.
Breakpoint 3 at 0x100000f07: file test.c, line 13.
(gdb) r
Starting program: /Users/lee/MyProgram/test/test
Breakpoint 2, main (argc=1, argv=0x7fff5fbff9d0) at test.c:13
13 t.c[0] = 10;
(gdb) p t
$5 = {
a = 1606416824,
c = "??"
}
(gdb) x/4xb &t
0x7fff5fbff9a0: 0xb8 0xf9 0xbf 0x5f
(gdb) n
14 t.c[1] = 1;
(gdb) p t
$6 = {
a = 1606416650,
c = "\n?"
}
(gdb) x/4xb &t
0x7fff5fbff9a0: 0x0a 0xf9 0xbf 0x5f
(gdb) n
16 printf("%d\n", t.a);
(gdb) p t
$7 = {
a = 1606353162,
c = "\n\001"
}
(gdb) x/4xb &t
0x7fff5fbff9a0: 0x0a 0x01 0xbf 0x5f
(gdb) n
1606353162