当前位置:网站首页>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;
}
输出内容
边栏推荐
- JS form get form & get form elements
- Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
- What if the win11 screenshot key cannot be used? Solution to the failure of win11 screenshot key
- JS also exports Excel
- Run the command once per second in Bash- Run command every second in Bash?
- Oracle - views and sequences
- 深耕开发者生态,加速AI产业创新发展 英特尔携众多合作伙伴共聚
- [digital analog] source code of MATLAB allcycles() function (not available before 2021a)
- Vscode 如何使用内置浏览器?
- 软件测试之网站测试如何进行?测试小攻略走起!
猜你喜欢
Introduction to the PureMVC series
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
Meow, come, come: do you really know if, if else
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot microservice code analysis and dialogue experim
Mathematical analysis_ Notes_ Chapter 10: integral with parameters
Camera calibration (I): robot hand eye calibration
Vscode automatically adds a semicolon and jumps to the next line
Lessons and thoughts of the first SQL injection
Basic idea of counting and sorting
The root file system of buildreoot prompts "depmod:applt not found"
随机推荐
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
两个div在同一行,两个div不换行「建议收藏」
Section 1: (3) logic chip process substrate selection
What is Web3
What work items do programmers hate most in their daily work?
Case reward: Intel brings many partners to promote the innovation and development of multi domain AI industry
jvm是什么?jvm调优有哪些目的?
【实践出真理】import和require的引入方式真的和网上说的一样吗
Depth first traversal template principle of tree and graph
Mathematical analysis_ Notes_ Chapter 10: integral with parameters
Network Security Learning - Information Collection
R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
Oracle -- 视图与序列
Poor math students who once dropped out of school won the fields award this year
过气光刻机也不能卖给中国!美国无理施压荷兰ASML,国产芯片再遭打压
On the 110th anniversary of Turing's birth, has the prediction of intelligent machine come true?
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
英特尔与信步科技共同打造机器视觉开发套件,协力推动工业智能化转型
Oracle - views and sequences
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice