C语言Day018 函数指针数组,特殊指针。

本文介绍了C语言中的函数指针数组,包括其本质和定义格式,并通过代码示例展示了如何使用。此外,还探讨了只读指针变量、只读变量指针和只读变量的概念及应用。最后,提到了两种特殊的指针类型:通过malloc动态分配的内存指针以及万能指针void*的使用原则。
摘要由CSDN通过智能技术生成

思维导图:

 

一.函数指针数组:

                 归根结底函数指针数组,其本质还是一个数组。

                 数组里每个元素都是一个函数指针。

                返回值类型的定义格式:

                int(*p[10])(int,int)

代码演示:


int sub(int a,int b);
int calc(int(*p)(int ,int),int a,int b);
int remind(int a,int b);
int mul(int a,int b);
int chu(int a,int b);
int add_3(int a,int b,int c);

int main()
{
	int a = 20;
	int b = 10;
	int c = 30;
	int ret;
	int (*p)(int,int);
	
	p = add;
	printf("%d\n",p(a,b));
	
	p = sub;
	printf("%p\n",p);
	printf("%d\n",p(a,b));
	
	int ret = calc(add,a,b);
	printf("%d\n",ret);
	
	ret = calc(sub,a,b);
	int x = calc(add,a,b);
	ret = calc(add,x,c);
	printf("%d\n",ret);
	
	p = mul;
	printf("%d\n",p(a,b));
	
	p = chu;
	printf("%d\n",p(a,b));
	
	p = remind;
	printf("%d\n",p(a,b));
	return 0;
	
	
}

int add(int a,int b)
{
	int ret;
	ret = a+b;
	return a+b;
}

int sub(int a,int b)
{
	return a-b;
}
int calc(int(*p)(int ,int),int a,int b)
{
	int ret;
	ret = p(a,b);
	return ret;
	
}
int mul(int a,int b)
{
	return a*b;
}

int chu(int a,int b)
{
	return a/b;
}

int remind(int a,int b)
{
	return a%b;
}

int add_3(int a,int b,int c)
{
	return a+b+c;
}



//函数指针数组

#include<stdio.h>

int main()
{
	int a = 4396;
	int b = 2200;
	
	//定义一个函数指针数组 
	int (*p[5])(int,int) = {add,sub,mul,chu,remind};//数组有五个元素, 
	int (*p[5])(int,int);
	p[0] = add;
	p[1] = sub;
	p[2] = mul;
	p[3] = chu;
	p[4] = remind;
	
	int i;
	for(i=0;i<5;i++){
		printf("%d\n",p[i](a,b));
	}

	return 0;
 } 


//只读指针变量 
#include<stdio.h>
int main()
{
	int a = 30;
	int b = 50;
	int * const p = &a;
	printf("%p\n",p);
	printf("%d\n",*p);
	
	p= &b;//用const 修饰的指针变量不能被修改 
	printf("%p\n",p);
	printf("%d\n",*p);

	*p = 600;// 其中的变量可以被改变。 
	printf("%d\n",a);
	printf("%p\n",p);
	printf("%d\n",*p); 
	return 0;
}

从下标0开始到p[9],包含的每个元素都指针变量,指向的函数要具备有整形的返回值和两个整形的参数的两个条件。

二. 只读指针变量:

概念:只读指针变量不能重新赋值,并且地址不能被修改,值可以被修改

定义:类型 * const +变量名;

代码演示:

//只读指针变量 
#include<stdio.h>
int main()
{
	int a = 30;
	int b = 50;
	int * const p = &a;
	printf("%p\n",p);
	printf("%d\n",*p);
	
	//p= &b;//用const 修饰的指针变量不能被修改 
	//printf("%p\n",p);
	//printf("%d\n",*p);// 这段代码编译运行的时候会报错。

	*p = 600;// 其中的变量可以被改变。 
	printf("%d\n",a);
	printf("%p\n",p);
	printf("%d\n",*p); 
	return 0;
}

WARNING:p已经指向&a了,并且const相当于锁住了,就不能指向&b了,否则编译器会报错。

三.只读变量指针:

概念:其地址允许被修改,但其变量不能被修改。

定义:类型  const *变量名;

代码演示:

//只读变量指针 
#include<stdio.h>
int main()
{
	int a = 200;
	int b = 105;
	int const *p = &a;//地址可以被修改 
	printf("%d\n",*p);
	printf("%p\n",p);
	printf("%d\n",a);
	
	p = &b;
	printf("%d\n",*p);
	printf("%p\n",p);
	printf("%d\n",a);
	
	*p = 300; //变量的值不能被修改 
	printf("%d",*p); 
	printf("%p",*p); 
	printf("%d",a); 
	return 0; 
}

四.只读变量:

概念:使用两个const修饰,及不能改变已经完成指向的地址,更不能改变其值

代码演示:

//只读变量 
#include<stdio.h>
int main()
{
	int a = 20;
	int b = 10;
	int const * const p = &a;
	printf("%p\n",p); 
	printf("%d\n",*p); 
	
	p = &b; //后面修饰的const不能被修改 
	*p = 100;//依然不能被修改。 
	printf("%p\n",p); 
	printf("%d\n",*p); 
	return 0;
 } 

五.特殊指针:

1.malloc

概念:int a = 10;//系统自己分配,申请的空间在栈上

           p = malloc(512);//需要程序员自己分配,用完释放掉,申请的空间是在堆上 

代码演示:

#include<stdio.h>
#include<stdlib.h>
int main()
{
	int *p;
//	int a = 10;//系统自己分配,申请的空间在栈上 
	p = malloc(512);//需要程序员自己分配,用完释放掉,申请的空间是在堆上 
	if(p==NULL)
	{
		return -1;
	}
	*p = 100;
	printf("%d\n",*p);
	free(p);
	return 0;
}

2.万能指针:

概念:void* 类型指针使用原则是: 必须要做强制类型转换成为目标类型后才可以使用。

代码演示:

//特殊指针; 
int main()
{
	int a = 10;
	char b = 'b';
	float c = 3.141;
	double e = 3.5645;
	
	void *pp;
//	pp = &a;
//	pp = &b;
	pp = &c;
//	pp = &e;
	
	printf("%p\n",pp);
	printf("%.2f\n",*(float*)(pp));
	
	return 0;
 } 

还是一如既往的绕...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值