指针(三)
- 数组作为参数
- 通过char指针遍历数据.
- 通过short指针遍历数据.
- 通过int指针遍历数据.
一、数组作为参数
1 #include "stdafx.h"
2
3 /*
4 * 函数参数既可以是int arr[],也可以是int* arr
5 */
6 void function(int arr[], int length)
7 {
8 int* parr = arr;
9 for (int i = 0; i < length; i ++)
10 {
11 printf("%d ", *(parr + i));
12 }
13 printf("\n");
14 }
15
16 int main(int argc, char* argv[])
17 {
18 //定义一个一维数组
19 int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
20 /*
21 * 将数组的首地址以及数组的长度传入函数中,既可以使用arr直接作为数组的首地址,也
22 * 可以传这个&arr[0]作为数组的首地址
23 */
24 function(arr, 10);
25 return 0;
26 }
模拟实现CE的数据搜索功能:
这一堆数据中存储了角色的血值信息,假设血值的类型为int类型,值为100(10进制)
请列出所有可能的值以及该值对应的地址.
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,0x10,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
二、通过char指针遍历数据
1 #include "stdafx.h"
2 char arr[100] =
3 {
4 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
5 0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
6 0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,
7 0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
8 0x00,0x00,0x64,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
9 0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
10 0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
11 0x00,0x02,0x74,0x0F,0x41,0x00,0x06,0x08,0x00,0x00,
12 0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00,
13 0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00
14 };
15
16 void FindBlood()
17 {
18 char* parr = arr;
19 //也可以写成下面这样子的
20 //char* parr = &arr[0];
21
22 for (int i = 0; i < 100; i ++)
23 {
24 if (*(parr + i) == 0x64)
25 {
26 printf("%d\t%x\n", *(parr + i), (parr + i));
27 }
28 }
29 }
30
31 int main(int argc, char* argv[])
32 {
33 FindBlood();
34 return 0;
35 }
对应的值以及它对应的地址答应如下
三、通过short指针遍历数据
1 #include "stdafx.h"
2 char arr[100] =
3 {
4 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
5 0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
6 0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,
7 0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
8 0x00,0x00,0x64,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
9 0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
10 0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
11 0x00,0x02,0x74,0x0F,0x41,0x00,0x06,0x08,0x00,0x00,
12 0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00,
13 0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00
14 };
15
16 void ShortFindBlood()
17 {
18 //定义一个short* 指针,指向数组的首地址
19 short* p = (short*)arr;
20 //定义一个char* 指针,指向数组的首地址,这个变量的作用是,使用地址遍历时,每次只移动一个,如果是short*,那每次移动就是2
21 char* parr = (char*)arr;
22 //打印一下数组的首地址
23 printf("%x\n", arr);
24
25 //遍历
26 for (int i = 0; i < 99; i ++)
27 {
28 //每次只移动一个地址编号后利用强转将该地址传递给short* 变量p
29 p = (short*)(parr + i);
30 //如果*p 等于 0x64(十进制的100)
31 if (*p == 0x64)
32 {
33 //打印出这个值,并打印出这个值的地址值
34 printf("%d\t%x\n", *p, p);
35 }
36 }
37 }
38
39 int main(int argc, char* argv[])
40 {
41 ShortFindBlood();
42 return 0;
43 }
四、通过int指针遍历数据
1 #include "stdafx.h"
2 char arr[100] =
3 {
4 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x07,0x09,
5 0x00,0x20,0x10,0x03,0x03,0x0C,0x00,0x00,0x44,0x00,
6 0x00,0x33,0x00,0x47,0x0C,0x0E,0x00,0x0D,0x00,0x11,
7 0x00,0x00,0x00,0x02,0x64,0x00,0x00,0x00,0xAA,0x00,
8 0x00,0x00,0x64,0x10,0x00,0x00,0x00,0x00,0x00,0x00,
9 0x00,0x00,0x02,0x00,0x74,0x0F,0x41,0x00,0x00,0x00,
10 0x01,0x00,0x00,0x00,0x05,0x00,0x00,0x00,0x0A,0x00,
11 0x00,0x02,0x74,0x0F,0x41,0x00,0x06,0x08,0x00,0x00,
12 0x00,0x00,0x00,0x64,0x00,0x0F,0x00,0x00,0x0D,0x00,
13 0x00,0x00,0x23,0x00,0x00,0x64,0x00,0x00,0x64,0x00
14 };
15
16 void IntFindBlood()
17 {
18 //定义一个int* 指针,指向数组的首地址
19 int* p = (int*)arr;
20 //定义一个char* 指针,指向数组的首地址,这个变量的作用是,使用地址遍历时,每次只移动一个,如果是int*,那每次移动就是4
21 char* parr = (char*)arr;
22 //打印一下数组的首地址
23 printf("%x\n", arr);
24
25 //遍历
26 for (int i = 0; i < 97; i ++)
27 {
28 //每次只移动一个地址编号后利用强转将该地址传递给int* 变量p
29 p = (int*)(parr + i);
30 //如果*p 等于 0x64(十进制的100)
31 if (*p == 0x64)
32 {
33 //打印出这个值,并打印出这个值的地址值
34 printf("%d\t%x\n", *p, p);
35 }
36 }
37 }
38
39 int main(int argc, char* argv[])
40 {
41 IntFindBlood();
42 return 0;
43 }