64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
int main()
{
unsigned int a = 0xFFFFFFF7;
unsigned char i = (unsigned char)a;
char* b = (char*)&a;;
printf("%08x, %08x\n", i, *b);
}
unsigned char i = (unsigned char)a;
char* b = (char*)&a;
printf("%08x, %08x\n", i, *b);
supertool@supertool-desktop:~/test$ ./unsigned
000000f7, fffffff7
supertool@supertool-desktop:~/test$ gdb ./unsigned
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/supertool/test/unsigned...done.
(gdb) l
1 #include <iostream>
2 #include <cstdio>
3 using namespace std;
4
5 int main()
6 {
7 unsigned int a = 0xFFFFFFF7;
8 unsigned char i = (unsigned char)a;
9 char* b = (char*)&a;;
10 printf("%08x, %08x\n", i, *b);
(gdb) b 10
Breakpoint 1 at 0x400751: file unsigned.cpp, line 10.
(gdb) r
Starting program: /home/supertool/test/unsigned
Breakpoint 1, main () at unsigned.cpp:10
10 printf("%08x, %08x\n", i, *b);
(gdb) x /x &a
0x7fffffffe248: 0xfffffff7
(gdb) x /x &i
0x7fffffffe24f: 0x000000f7
(gdb) x /x &b //通过这条命令我们可以看到b保存是&a,而&a保存是0xfffffff7
0x7fffffffe240: 0xffffe248
(gdb) x /x b
0x7fffffffe248: 0xfffffff7
(gdb) n
000000f7, fffffff7
11 }
(gdb)
000000f7, fffffff7
请按任意键继续. . .
000000f7, 000000f7
请按任意键继续. . .
int d = 10000;
char *f = (char*)(&d);
cout<<*f<<endl;
b = &a; //传a地址
*b 是地址的值