当前位置:网站首页>C语言中函数指针与指针函数
C语言中函数指针与指针函数
2022-07-06 22:21:00 【小浩编程】
数组指针
#include <stdio.h>
int getTheData(int (*p)[4],int hang,int lie)//(*p)[4]是数组指针
{
int data;
data = *(*(p+hang)+lie);//将对应数组中的值找出来
return data;
//return p[hang][lie];//同理 将对应数组中的值找出来
}
void tipsInputHangLie(int *pm, int *pn)//形参中的变量类型要与实参中相同,都是int
{
printf("输入行列值:\n");
scanf("%d%d",pm,pn);//指针就是地址,对相应的地址进行操作
puts("done!");
}
//arr,arr[0]
int main()
{
int arr[3][4] = {
{
11,22,33,44},{
12,13,15,16},{
22,66,77,88}};//arr+
int ihang,ilie;
int data;
//1. 提示用户输入行列值
tipsInputHangLie(&ihang,&ilie);//对地址进行操作,所以函数中的操作能改变ihang和ilie的值
//2. 找出对应行列值的那个数
data = getTheData(arr,ihang,ilie);//arr是数组的地址,也就是指针
//3. 打印出来
printf("%d行%d列的值是%d\n",ihang,ilie,data);
}
输出内容
函数指针
回掉函数的底层逻辑就是函数指针
#include <stdio.h>
#include <stdlib.h>
int getMax(int data1, int data2)
{
return data1>data2 ? data1:data2;
}
int getMin(int data1, int data2)
{
return data1<data2 ? data1:data2;
}
int getSum(int data1, int data2)
{
return data1+data2;
}
int dataHandler(int data1, int data2, int (*pfunc)(int, int ))//第三个形参是函数指针,函数指针中对类型有要求,形参名可省略
{
int ret;
ret = (*pfunc)(data1,data2);//调取函数,求得返回值
return ret;
}
int main()
{
int a = 10;
int b = 20;
int cmd;
int ret;
int (*pfunc)(int , int );
printf("请输入1(取大值),2(取小值),或者3(求和)\n");
scanf("%d",&cmd);
switch(cmd){
case 1:
pfunc = getMax;//对地址进行了更改,也就变化了对应的值
break;
case 2:
pfunc = getMin;
break;
case 3:
pfunc = getSum;
break;
default:
printf("输入错误!@输入1(取大值),2(取小值),或者3(求和)\n");
exit(-1);//异常退出
break;
}
ret = dataHandler(a,b,pfunc);//pfunc获取了对应的函数名,第三个实参pfunc是函数指针的地址
printf("ret = %d\n",ret);
return 0;
}
输出内容
函数指针数组
#include <stdio.h>
#include <stdlib.h>
int getMax(int data1, int data2)
{
return data1>data2 ? data1:data2;
}
int getMin(int data1, int data2)
{
return data1<data2 ? data1:data2;
}
int getSum(int data1, int data2)
{
return data1+data2;
}
int main()
{
int a = 10;
int b = 20;
int ret;
int (*pfunc[3])(int , int )={
getMin,
getMax,
getSum};//函数指针数组!初始化,将数组中的值(这个值是个地址,也就是指针,这个指针是个函数指针,将三个函数地址放在数组里)
for(int i=0;i<3;i++){
ret = (*pfunc[i])(a,b);//遍历数组中的三个函数指针,获取返回值
printf("ret = %d\n",ret);
}
return 0;
}
输出内容
函数指针
#include <stdio.h>
int* getPosPerson(int pos, int (*pstu)[4])//函数指针,返回指针的函数
{
int *p;
p = (int *)(pstu+pos);//二维数组地址+输入的数就是对应的数组值,然后赋值给P
return p;
}
int main()
{
int scores[3][4]={
{
55,66,77,88},//学生1
{
66,55,99,100},//学生2
{
11,22,33,59},//学生3
};
int *ppos;
int pos;
printf("请输入你需要看的学生号数:0,1,2\n");
scanf("%d",&pos);
ppos = getPosPerson(pos, scores);//获取对应数组的地址
for(int i=0;i<4;i++){
//将小数组里面的值进行遍历出来输出
printf("%d ",*ppos++);//++代表偏移一个int整形的字节数
}
return 0;
}
输出内容
二级指针
#include <stdio.h>
void getPosPerson(int pos, int (*pstu)[4],int **ppos)//函数指针,返回指针的函数
{
*ppos = (int *)(pstu+pos);//用二级指针的目的是直接修改了MAIN函数中的ppos的值
}
int main()
{
int scores[3][4]={
{
55,66,77,88},
{
66,55,99,100},
{
11,22,33,59},
};
int *ppos;
int pos;
printf("请输入你需要看的学生号数:0,1,2\n");
scanf("%d",&pos);
getPosPerson(pos, scores,&ppos);
for(int i=0;i<4;i++){
printf("%d ",*ppos++);
}
return 0;
}
输出内容
边栏推荐
- AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
- 深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
- Gpt-3 is a peer review online when it has been submitted for its own research
- Some understandings about 01 backpacker
- 树与图的深度优先遍历模版原理
- MySQL null value processing and value replacement
- System framework of PureMVC
- Both primary and secondary equipment numbers are 0
- 【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
- 深入解析Kubebuilder
猜你喜欢
九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
AI landing new question type RPA + AI =?
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
Lessons and thoughts of the first SQL injection
Camera calibration (I): robot hand eye calibration
mpf2_ Linear programming_ CAPM_ sharpe_ Arbitrage Pricin_ Inversion Gauss Jordan_ Statsmodel_ Pulp_ pLU_ Cholesky_ QR_ Jacobi
AI 落地新题型 RPA + AI =?
Depth first traversal template principle of tree and graph
C # use Siemens S7 protocol to read and write PLC DB block
随机推荐
Terms used in the Web3 community
Flex layout and usage
What about the collapse of win11 playing pubg? Solution to win11 Jedi survival crash
mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi
Detect when a tab bar item is pressed
namespace基础介绍
Both primary and secondary equipment numbers are 0
树与图的深度优先遍历模版原理
Depth first traversal template principle of tree and graph
ESG Global Leaders Summit | Intel Wang Rui: coping with global climate challenges with the power of science and technology
程序员上班摸鱼,这么玩才高端!
Network Security Learning - Information Collection
Windows are not cheap things
Two methods of chromosome coordinate sequencing
JetBrain Pycharm的一系列快捷键
A simple and beautiful regression table is produced in one line of code~
MySQL split method usage
AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
Some understandings about 01 backpacker
In depth analysis of kubebuilder