当前位置:网站首页>Difference between function pointer and pointer function
Difference between function pointer and pointer function
2022-06-22 12:26:00 【Alen. Wang】
One 、【 A function pointer 】
While the program is running , Function code is the algorithmic instruction part of a program , Like arrays, they take up storage space , Have corresponding addresses . You can use a pointer variable to point to the first address of the array , You can also use a pointer variable to point to the first address of the function code , Pointer variables that point to the first address of the function code are called function pointers .
1、 Function pointer definition
seeing the name of a thing one thinks of its function , A function pointer is a pointer , But the function that this pointer points to , It is not an ordinary basic data type or class object .
“ Function type ” Describe the return type of the function , because “()” Has a higher priority than “*”, Therefore, the parentheses outside the pointer variable name are essential , hinder “ Parameter list ” Represents the parameter list of the function pointed to by the pointer variable .
for example :
int (*f)(int x);
double (*ptr)(double x); When defining function pointers, please pay attention to :
The number and type of parameters of the function pointer and the function it points to should be — To ; The type of function pointer and the return value type of function must also be consistent .
2、 Assignment of function pointer
The function name, like the array name, represents the first address of the function code , Therefore, when assigning values , Just point the function pointer to the function name . for example ,
int func(int x); /* Declare a function */
int (*f) (int x); /* Declare a function pointer */
f=func; /* take func The first address of the function is assigned to the pointer f */
In assignment, the function func Without brackets , And without parameters , because func Represents the first address of a function , So after assignment , The pointer f It points to the function func(x) The first address of the code .
3、 Call function through function pointer
Function pointer is called by function name and related parameters .
Similar to other pointer variables , If the pointer variable pi Is to point to an integer variable i The pointer to , be *p Equal to the variable it refers to i; If pf Is to point to a floating point variable f The pointer to , be *pf It is equivalent to the variable it refers to f. similarly ,*f It's a pointing function func(x) The pointer to , be *f It represents the function it points to func. So in the implementation of f=func; after ,(*f) and func The same function .
Because the function pointer points to a function in the storage area , Therefore, the corresponding function can be called through the function pointer . Now let's discuss how to call a function with a function pointer , It should perform the following three steps : First , To illustrate the function pointer variable .
for example :int (*f)(int x);
secondly , To assign a value to a function pointer variable .
for example : f=func; (func(x) There must be a definition )
Last , Use (* Pointer to the variable )( Parameter table ); Call function .
for example : (*f)(x);(x You have to assign a value first )
【 example 】 Any input n Number , Find the largest number , And output the maximum value .
main()
{
int f();
int i,a,b;
int (*p)(); /* Define function pointers */
scanf("%d",&a);
p=f; /* Give the function pointer p assignment , Make it point to a function f */
for(i=1;i<9;i++)
{
scanf("%d",&b);
a=(*p)(a,b); /* Through the pointer p Call function f */
}
printf("The Max Number is:%d",a)
}
f(int x,int y)
{
int z;
z=(x>y)?x:y;
return(z);
}
The running result is :
343 -45 4389 4235 1 -534 988 555 789 The Max Number is:4389
A function pointer , First of all, it's a pointer , It's just that this pointer points to a function . The pointer variable can point to the address of the variable 、 Array 、 character string 、 Dynamically assign addresses , It can also point to a function , Each function is compiled , The system will assign an entry address to the function , The function name indicates the entry address , Then a pointer variable that points to a function is called a function pointer variable .
When writing an embedded driver , You will encounter the following structure :
struct file_operations {
int (*seek) (struct inode * ,struct file *, off_t ,int);
int (*read) (struct inode * ,struct file *, char ,int);
int (*write) (struct inode * ,struct file *, off_t ,int);
int (*readdir) (struct inode * ,struct file *, struct dirent * ,int);
int (*select) (struct inode * ,struct file *, int ,select_table *);
int (*ioctl) (struct inode * ,struct file *, unsined int ,unsigned long);
int (*mmap) (struct inode * ,struct file *, struct vm_area_struct *);
int (*open) (struct inode * ,struct file *);
int (*release) (struct inode * ,struct file *);
int (*fsync) (struct inode * ,struct file *);
int (*fasync) (struct inode * ,struct file *,int);
int (*check_media_change) (struct inode * ,struct file *);
int (*revalidate) (dev_t dev);
}
The structure above file_operations The components inside are A function pointer :int (*read) (struct inode * ,struct file *, char ,int); Be careful int (*read) It is bracketed .
Two 、【 Pointer function 】
A function can not only bring back the value of an integer data , Character type values and real type values , You can also bring back pointer type data , Make it point to an address unit .
Functions that return pointers , The general definition format is :
Type identifier * Function name ( Parameter table )
int *f(x,y);
among x,y It's a formal parameter ,f Is the function name , The call returns an address pointer to integer data .f(x,y) Is the function , Its value is a pointer .
Be careful , This function has a return value , The return value is int *, That is, the return value is of pointer type .
Such as :char *ch(); A function that returns a character pointer , Look at the following example :【 example 】 The string 1(str1) Copy to string 2(str2), And output string 2.
#include "stdio.h"
main()
{
char *ch(char *,char *);
char str1[]="I am glad to meet you!";
char str2[]="Welcom to study C!";
printf("%s",ch(str1,str2));
}
char *ch(char *str1,char *str2)
{
int i;
char *p;
p=str2
if(*str2==NULL) exit(-1);
do
{
*str2=*str1;
str1++;
str2++;
}
while(*str1!=NULL);
return(p);
}
Through analysis, we can conclude that :
A function pointer is a pointer to a function , The pointer function only indicates that it is a function whose return value is a pointer , Function pointers can be used to point to a function .
边栏推荐
- Nansen年度报告
- [high frequency written test questions] 513 Find the value in the lower left corner of the tree
- Yunshang people and IOT technology joined the dragon lizard community to jointly create a software and hardware service ecosystem
- Messari年度报告-2021
- Markov chain, hidden Markov model
- 酷开电视adb
- 0179-Largest Number( 最大数)
- Redis - 3. Publish and subscribe
- 论文解读——Performance of Recommender Algorithms on Top-N Recommendation Tasks
- input输入框只能输入,0-100之间的数组,保留两位小数
猜你喜欢

Markov chain, hidden Markov model

【云原生 | Kubernetes篇】Kubernetes简介(一)

4tb production database cannot be accessed due to disk rejecting i/o to offline device failure

初始transformer需要了解的一些概念

About cache exceptions: solutions for cache avalanche, breakdown and penetration

Redis - 7. Transaction operation

Find all prime numbers between 100 and 200

Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation

TIS教程04-客户端

Some concepts that the initial transformer needs to understand
随机推荐
In C # development, the third-party components lambdaparser, dynamicexpresso and z.expressions are used to dynamically parse / evaluate string expressions
Struggle, programmer -- Chapter 41 all kinds of things today are like water without trace; On the eve of the Ming Dynasty, you are a stranger
SQl、Oracle剔除重复记录的语句
Ogg12 processing ogg-01163 fault error
Redis - 5. Jedis operation redis6
New progress in the construction of meituan's Flink based real-time data warehouse platform
Messari年度报告-2021
jmeter生成测试报告
OGG12处理OGG-01163故障错误
Redis - 9. Persistent AOF (append onlyfile)
《Go题库·10》channel和锁的对比
JMeter generates test reports
Summary of SQL injection bypass methods
The input input box can only input an array between 0 and 100, with two decimal places reserved
Error: unresolved variable $bus and "typeerror: cannot read property '$on' of undefined“
Redis - 10. Master slave replication
oracle游标
Nansen年度报告
职场杰出人士的20个好习惯
[cloud native | kubernetes] Introduction to kubernetes (I)