关于指针

下面是指针的总结的思维导图

下面我们对

指针数组、数组指针、 函数指针、 函数指针数组、 指向函数指针数组的指针 做详细的理解

(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;
}

注:这部分的练习题一定要结合图来解答

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值