指针6

数组指针

本质就是一个指针

多维数组指针指向一维数组

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
#include "stdafx.h"

char arr[] = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,
0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
0x00,0x00,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
0x00,0x02,0x74,0x0F,0x41,0x00,0x06,0x08,0x00,0x00,
0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00,
0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00
};

int main(int argc, char* argv[])
{
char (*pa)[2][3][3];

pa=(char (*)[2][3][3])arr;

printf("%x",*(*(*(*(pa+2)+3)+2)+2));
//pa的宽度为,char类型指针去掉一个星号,1*2*3*3=18,加2就是18*2=36字节
//*(pa+2)的宽度为,1*3*3=9,加3就是3*9=27
//*(*(pa+2)+3的宽度为,1*3=3,加2就是6
//最后加2,最终结果是0x02

getchar();
return 0;

}

image-20231113105730882

函数指针

既然是指针类型,恒定长度为4字节,函数指针无法进行加法运算,因为函数的宽度无法确定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"

int x=10;

int fun(int x,int y)
{
return x+y;
}

int main(int argc, char* argv[])
{

int (*pfun)(int,int);



printf("%d",sizeof(pfun));

getchar();
return 0;

}

image-20231113105913254

通过数据区存放函数

把原加法函数的硬编码复制出去

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
// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int x=10;

unsigned char arr[]=
{
0x55,
0x8B, 0xEC,
0x83, 0xEC, 0x40,
0x53,
0x56,
0x57,
0x8D, 0x7D, 0xC0,
0xB9, 0x10, 0x00, 0x00, 0x00,
0xB8, 0xCC, 0xCC, 0xCC, 0xCC,
0xF3, 0xAB,
0x8B, 0x45, 0x08,
0x03, 0x45, 0x0C,
0x5F,
0x5E,
0x5B,
0x8B, 0xE5,
0x5D,
0xC3
};

int main(int argc, char* argv[])
{

int (*p)(int,int);

p=(int (*)(int,int))&arr;

int a=p(5,6);

printf("%d",a);

getchar();
return 0;

}