当前位置:网站首页>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;
}
输出内容
边栏推荐
- 史上最全学习率调整策略lr_scheduler
- Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
- Flex layout and usage
- MySQL forgot how to change the password
- ACL2022 | 分解的元学习小样本命名实体识别
- Tiktok may launch an independent grass planting community platform: will it become the second little red book
- 抖音或将推出独立种草社区平台:会不会成为第二个小红书
- How to conduct website testing of software testing? Test strategy let's go!
- AI表现越差,获得奖金越高?纽约大学博士拿出百万重金,悬赏让大模型表现差劲的任务
- Two methods of chromosome coordinate sequencing
猜你喜欢

Basic idea of counting and sorting
![A detailed explanation of head pose estimation [collect good articles]](/img/22/7ae0b12c3d945b449bcc8bb4a8961b.jpg)
A detailed explanation of head pose estimation [collect good articles]

Camera calibration (I): robot hand eye calibration

A line of R code draws the population pyramid

System framework of PureMVC

AI 落地新题型 RPA + AI =?
![Local tool [Navicat] connects to remote [MySQL] operation](/img/e8/a7533bac4a70ab5aa3fe15f9b0fcb0.jpg)
Local tool [Navicat] connects to remote [MySQL] operation
![[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet](/img/58/4337f0972f7171a5c21e640f03e0b7.png)
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet

Meow, come, come: do you really know if, if else

DFS和BFS概念及实践+acwing 842 排列数字(dfs) +acwing 844. 走迷宫(bfs)
随机推荐
System framework of PureMVC
DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
Nanopineo use development process record
Common methods of list and map
Is there any way to bookmark the code in the visual studio project- Is there a way to bookmark code in a Visual Studio project?
各路行业大佬称赞的跨架构开发“神器”,你get同款了吗?
Gpt-3 is a peer review online when it has been submitted for its own research
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
Implementation of JSTL custom function library
Acl2022 | decomposed meta learning small sample named entity recognition
Fiance donated 500million dollars to female PI, so that she didn't need to apply for projects, recruited 150 scientists, and did scientific research at ease!
案例大赏:英特尔携众多合作伙伴推动多领域AI产业创新发展
【數模】Matlab allcycles()函數的源代碼(2021a之前版本沒有)
Deeply cultivate the developer ecosystem, accelerate the innovation and development of AI industry, and Intel brings many partners together
Depth first traversal template principle of tree and graph
sscanf,sscanf_ S and its related usage "suggested collection"
掌握软件安全测试方法秘笈,安全测试报告信手捏来
关于01背包个人的一些理解
Break the memory wall with CPU scheme? Learn from PayPal to expand the capacity of aoteng, and the volume of missed fraud transactions can be reduced to 1/30
Some understandings about 01 backpacker