当前位置:网站首页>Function pointer and pointer function in C language
Function pointer and pointer function in C language
2022-07-07 04:55:00 【Xiaohao programming】
Array pointer
#include <stdio.h>
int getTheData(int (*p)[4],int hang,int lie)//(*p)[4] It's an array pointer
{
int data;
data = *(*(p+hang)+lie);// Find the value in the corresponding array
return data;
//return p[hang][lie];// Empathy Find the value in the corresponding array
}
void tipsInputHangLie(int *pm, int *pn)// The variable type in the formal parameter should be the same as that in the argument , All are int
{
printf(" Enter row and column values :\n");
scanf("%d%d",pm,pn);// The pointer is the address , Operate on the corresponding address
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. Prompt the user to enter row and column values
tipsInputHangLie(&ihang,&ilie);// Operate on the address , So the operation in the function can change ihang and ilie Value
//2. Find the number corresponding to the row and column value
data = getTheData(arr,ihang,ilie);//arr Is the address of the array , That's the pointer
//3. Print out
printf("%d That's ok %d Is the value of the column %d\n",ihang,ilie,data);
}
Output content 
A function pointer
The underlying logic of returning a function is the function pointer
#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 ))// The third parameter is the function pointer , There are requirements for types in function pointers , The formal parameter name can be omitted
{
int ret;
ret = (*pfunc)(data1,data2);// Call function , Get the return value
return ret;
}
int main()
{
int a = 10;
int b = 20;
int cmd;
int ret;
int (*pfunc)(int , int );
printf(" Please enter 1( Take a big value ),2( Take a small value ), perhaps 3( Sum up )\n");
scanf("%d",&cmd);
switch(cmd){
case 1:
pfunc = getMax;// The address has been changed , It changes the corresponding value
break;
case 2:
pfunc = getMin;
break;
case 3:
pfunc = getSum;
break;
default:
printf(" Input error !@ Input 1( Take a big value ),2( Take a small value ), perhaps 3( Sum up )\n");
exit(-1);// Abnormal exit
break;
}
ret = dataHandler(a,b,pfunc);//pfunc Get the corresponding function name , The third argument pfunc Is the address of the function pointer
printf("ret = %d\n",ret);
return 0;
}
Output content 
Function pointer array
#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};// Function pointer array ! initialization , Put the values in the array ( This value is an address , That's the pointer , This pointer is a function pointer , Put the three function addresses in the array )
for(int i=0;i<3;i++){
ret = (*pfunc[i])(a,b);// Traverse the three function pointers in the array , Get the return value
printf("ret = %d\n",ret);
}
return 0;
}
Output content 
A function pointer
#include <stdio.h>
int* getPosPerson(int pos, int (*pstu)[4])// A function pointer , Functions that return pointers
{
int *p;
p = (int *)(pstu+pos);// Two dimensional array address + The number entered is the corresponding array value , And then assign it to P
return p;
}
int main()
{
int scores[3][4]={
{
55,66,77,88},// Student 1
{
66,55,99,100},// Student 2
{
11,22,33,59},// Student 3
};
int *ppos;
int pos;
printf(" Please enter the number of students you need to see :0,1,2\n");
scanf("%d",&pos);
ppos = getPosPerson(pos, scores);// Get the address of the corresponding array
for(int i=0;i<4;i++){
// Traverse the values in the small array and output
printf("%d ",*ppos++);//++ Represents an offset int Integer bytes
}
return 0;
}
Output content 
The secondary pointer
#include <stdio.h>
void getPosPerson(int pos, int (*pstu)[4],int **ppos)// A function pointer , Functions that return pointers
{
*ppos = (int *)(pstu+pos);// The purpose of using secondary pointer is to directly modify MAIN Function ppos Value
}
int main()
{
int scores[3][4]={
{
55,66,77,88},
{
66,55,99,100},
{
11,22,33,59},
};
int *ppos;
int pos;
printf(" Please enter the number of students you need to see :0,1,2\n");
scanf("%d",&pos);
getPosPerson(pos, scores,&ppos);
for(int i=0;i<4;i++){
printf("%d ",*ppos++);
}
return 0;
}
Output content 
边栏推荐
- Factor analysis r practice (with R installation tutorial and code)
- Basic idea of counting and sorting
- 【ArcGIS教程】专题图制作-人口密度分布图——人口密度分析
- Field data acquisition and edge calculation scheme of CNC machine tools
- Canteen user dish relationship system (C language course design)
- Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
- Station B boss used my world to create convolutional neural network, Lecun forwarding! Burst the liver for 6 months, playing more than one million
- DFS and BFS concepts and practices +acwing 842 arranged numbers (DFS) +acwing 844 Maze walking (BFS)
- How does vscade use the built-in browser?
- File upload vulnerability summary
猜你喜欢

Windows are not cheap things

Programmers go to work fishing, so play high-end!

namespace基础介绍
![[Yugong series] go teaching course 005 variables in July 2022](/img/29/2bb30443e1e418556b5e08932f75b4.png)
[Yugong series] go teaching course 005 variables in July 2022

Field data acquisition and edge calculation scheme of CNC machine tools

AI landing new question type RPA + AI =?

九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!

mpf2_线性规划_CAPM_sharpe_Arbitrage Pricin_Inversion Gauss Jordan_Statsmodel_Pulp_pLU_Cholesky_QR_Jacobi

Win11 control panel shortcut key win11 multiple methods to open the control panel
![Local tool [Navicat] connects to remote [MySQL] operation](/img/e8/a7533bac4a70ab5aa3fe15f9b0fcb0.jpg)
Local tool [Navicat] connects to remote [MySQL] operation
随机推荐
How to package the parsed Excel data into objects and write this object set into the database?
Leetcode notes
一度辍学的数学差生,获得今年菲尔兹奖
R descriptive statistics and hypothesis testing
Have you got the same "artifact" of cross architecture development praised by various industry leaders?
acwing 843. N-queen problem
MySQL数据库(基础篇)
史上最全学习率调整策略lr_scheduler
A row of code r shows the table of Cox regression model
Appium practice | make the test faster, more stable and more reliable (I): slice test
Intel David tuhy: the reason for the success of Intel aoten Technology
Gpt-3 is a peer review online when it has been submitted for its own research
一图看懂!为什么学校教了你Coding但还是不会的原因...
Lecture 3 of "prime mover x cloud native positive sounding, cost reduction and efficiency enhancement lecture" - kubernetes cluster utilization improvement practice
食堂用户菜品关系系统(C语言课设)
Oracle - views and sequences
指针与数组在函数中输入实现逆序输出
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
In depth analysis of kubebuilder
Meaning of 'n:m' and '1:n' in database design