下面是指针的总结的思维导图
下面我们对
指针数组、数组指针、 函数指针、 函数指针数组、 指向函数指针数组的指针 做详细的理解
(1)指针数组
int *arr1[10];
char *arr2[4];
char **arr3[5];
(2)数组指针
int(*p)[10];
//解释:p先和*结合,说明p是一个指针变量,然后指着指向的是一个大小为10个整型的数组。所以p是一个指针,指向一个数组,所以叫数组指针
(3)函数指针
void (*pfun1)();
(4)函数指针数组
int(*parr1[10])();
解释:parr1先和[ ]结合,说明parr1是数组,数组内容是int (*)( )类型的函数指针
<span style="color:#ff6666">
</span>
用途:转移表(计算器)
<span style="color:#ff6666">
</span>
下面这个代码是实现计算器的
#include<stdio.h>
#include<windows.h>
int add(int a,int b)
{
return a + b;
}
int sub(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int drv(int a, int b)
{
return a / b;
}
int main()
{
int x;
int y;
int input = 1;
int ret = 0;
int(*p[5])(int x, int y) = { 0, add, sub, mul, drv };
while (input)
{
printf("*************************************\n");
printf("*******1:add 2:sub*******\n");
printf("*******3:mul 4:div*******\n");
printf("*************************************\n");
printf("please choose:\n");
scanf_s("%d", &input);
if ((input<4 && input>1))
{
printf("请输入操作数:");
scanf_s("%d %d", &x, &y);
ret = (*p[input])(x, y);
}
else
{
printf("输入有误\n");
}
printf("%d\n", ret);
}
system("pause");
return 0;
}
(5)指向函数指针数组的指针
#include<stdio.h>
#include<windows.h>
void test(const char* str)
{
printf("%s\n", str);
}
int main()
{
//函数指针pfun
void(*pfun)(const char*) = test;
//函数指针的数组pfunarr
void(*pfunarr[5]) (const char *str);
pfunarr[0] = test;
//指向函数指针的数组pfunarr的指针ppfunarr
void(*(*ppfunarr)[10]) (const char*) = &pfunarr;
system("pause");
return 0;
}
注:这部分的练习题一定要结合图来解答