当前位置:网站首页>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;
}
输出内容
边栏推荐
- How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
- Terms used in the Web3 community
- 赠票速抢|行业大咖纵论软件的质量与效能 QECon大会来啦
- ACL2022 | 分解的元学习小样本命名实体识别
- Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
- 广告归因:买量如何做价值衡量?
- Nanopineo use development process record
- 当 Knative 遇见 WebAssembly
- 日常工作中程序员最讨厌哪些工作事项?
- 计数排序基础思路
猜你喜欢
Network Security Learning - Information Collection
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
A detailed explanation of head pose estimation [collect good articles]
In depth analysis of kubebuilder
Fix the problem that the highlight effect of the main menu disappears when the easycvr Video Square is clicked and played
Section 1: (3) logic chip process substrate selection
Introduction to namespace Basics
offer如何选择该考虑哪些因素
树与图的深度优先遍历模版原理
A row of code r shows the table of Cox regression model
随机推荐
组织实战攻防演练的5个阶段
Digital chemical plants realize the coexistence of advantages of high quality, low cost and fast efficiency
史上最全学习率调整策略lr_scheduler
sscanf,sscanf_ S and its related usage "suggested collection"
A detailed explanation of head pose estimation [collect good articles]
过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
jvm是什么?jvm调优有哪些目的?
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
namespace基础介绍
Tiktok may launch an independent grass planting community platform: will it become the second little red book
ESG全球领导者峰会|英特尔王锐:以科技之力应对全球气候挑战
軟件測試之網站測試如何進行?測試小攻略走起!
一图看懂!为什么学校教了你Coding但还是不会的原因...
【736. Lisp 语法解析】
Vscode automatically adds a semicolon and jumps to the next line
C # use Siemens S7 protocol to read and write PLC DB block
窗口可不是什么便宜的东西
Analyse approfondie de kubebuilder
This "advanced" technology design 15 years ago makes CPU shine in AI reasoning
In depth analysis of kubebuilder