目录

指针读写内存

指针读写内存

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <cstdio>
#define BASE 0x00417000

int g_a = 111;	//0x00417000
int g_b = 222;	//0x00417004
int g_c = 333;	//0x00417008

typedef struct T3
{
	int a1;	//偏移0
	int a2;	//偏移4
	int a3;	//偏移8
}TYPE3;
int main(int argc, char const *argv[])
{
	//指针读内存
	int* p1=(int*)0x00417000;
	printf("A:%d,%d,%d \n",g_a,g_b,g_c );	
	//A:111,222,333
	printf("B:%d,%d,%d \n",*(p1+0),*(p1+1),*(p1+2));	
	//B:111,222,333
	printf("C:%d,%d,%d \n",p1[0],p1[1],p1[2]);	
	//C:111,222,333
	printf("D:%d,%d,%d \n",*(int*)0x0041700,*(int*)0x00417004,*(int*)0x0041708);	
	//D:111,222,333

	//用指针向内存地址写入数据
	*(p1+2)=11567;
	printf("A:%d,%d,%d \n",g_a,g_b,g_c );
	//A:111,222,11567
	p1[2]=22567;
	printf("B:%d,%d,%d \n",g_a,g_b,g_c );
	//B:111,222,22567
	*(int*)(0x0041700+8)=333678;
	printf("C:%d,%d,%d \n",g_a,g_b,g_c );
	//C:111,222,333678

	TYPE3* p3 =(TYPE3*)0x00417000;
	printf("%p,%p,%p \n",&p3->a1,&p3->a2,&p3->a3);
	//111,222,333678
	printf("%d,%d,%d \n",p3->a1,p3->a2,p3->a3);
	//111,222,333678

	printf("p3=%p,p3+1=%p,sizeof(TYPE3)=%X \n",p3,p3+1,sizeof(TYPE3));
	//p3=00417000,p3+1=0041700C,sizeof(TYPE3)=C
	
	printf("p3=%p,p3+2=%p,sizeof(TYPE3)=%X \n",p3,p3+2,sizeof(TYPE3));
	//p3=00417000,p3+2=00417018,sizeof(TYPE3)=C

	printf(">>>:p3+2=%X =%X \n",p3+2,0x00417000+sizeof(TYPE3)*2);
	//>>>:p3+2=417018 =417018

	printf(">>>:p3+2=%X =%X \n",p3+2,0x00417000+0xC*2);
	//>>>:p3+2=417018 =417018


	return 0;
}