#include <iostream>
#include <stdio.h>
#include <stdint.h>
using namespace std;
int main(int argc, char **argv)
{
uint32_t arr[3] = {0x01020304,1,2};
void *p = arr;
void *p1 = p + 2;
cout<<p<<":"<<p1<<endl;
cout<<hex<<*(int *)p<<endl;
cout<<hex<<*(int *)p1<<endl;
return 0;
}
编译:
test.cpp: In function 'int main(int, char**)':
test.cpp:12: warning: pointer of type 'void *' used in arithmetic
警告:使用void*指针加法
(gdb) print arr
$1 = {16909060, 1, 2}
(gdb) print p
$2 = (void *) 0x7fffffffe300
(gdb) print p1
$3 = (void *) 0x7fffffffe302
(gdb) print *p
Attempt to dereference a generic pointer.
(gdb) print *(char *)p
$4 = 4 '\004'
(gdb) print *(char *)p1
$5 = 2 '\002'
(gdb) print *(char *)(p1+1)
$6 = 1 '\001'
(gdb) print *(int *)p1
$7 = 65794
(gdb) x/10bx 0x7fffffffe300
0x7fffffffe300: 0x04 0x03 0x02 0x01 0x01 0x00 0x00 0x00
0x7fffffffe308: 0x02 0x00
(gdb) x/10bx
0x7fffffffe30a: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xe3
0x7fffffffe312: 0xff 0xff