当前位置:网站首页>Pointer - function pointer
Pointer - function pointer
2022-07-02 22:25:00 【It's Yi'an】
Catalog
1 What is a function pointer ?
2.2 Function () The meaning of
2.4 Assignment and use of function pointer
3 Two interesting function pointers
No.2 Simplification of ——typedef
1 What is a function pointer ?
Let's make an analogy
int* pa; // Integer pointer
char* pb; // Character pointer
int(*pc)[3]; // Array pointer , Point to storage 3 individual int Array of
char(*pd)[3]; // Array pointer , Point to storage 3 individual char Array of
seeing the name of a thing one thinks of its function , A function pointer is a pointer to a function
2 Function pointer standard
2.1 Meaning of function name
When we study arrays, we know that the array name indicates the address of the first element of the array ,& The array name represents the address of the entire array , Because arrays occupy a continuous space in space , So is the function . The function name represents the address of the function , But it is a little different from the array name :& Function name is equivalent to function name .
#include<stdio.h>
void aa(void) { // Definition aa function
printf("123");
}
int main(void) {
void(*p)(void); // Define a function pointer , Don't worry about this
p = aa;
printf("%p\n", p);
printf("%p\n", *p);
printf("%p\n", aa);
printf("%p\n", &aa); // The four results are exactly the same
}
2.2 Function () The meaning of
We usually call functions , Is a function name plus a (),() According to the situation, put the corresponding parameters . Remember the array 【】 Do you ? It means to dereference the pointer on the left after operation .() It means to access the address on the left after passing in parameters ( It doesn't work here * Visit it ,() The function is defined in the design ).
2.3 A function pointer
int Add(int a,int b){} // A function definition
int (*p)(int, int); // A function pointer variable definition ,p Precede * Union knowing is a pointer
// And again () combination , Indicates a pointing function .
// With the int,int The function pointed to by the combination representation has two parameters
// Finally and int combination , Indicates that the pointed function returns a int
// About why brackets should be added , because p It will precede the one on the right () The combination results in a return value of int* Function of type
// priority *<[]<()
2.4 Assignment and use of function pointer
When assigning an array pointer , We assign the address of the array to it , It can be considered as a type one level higher than the first level pointer (& Array name ). The same is true of function pointers , We assign the address of the function to the function pointer variable , Because it has no first element address , So it can be regarded as a first level pointer .
#include<stdio.h>
int Add(int a, int b) {
printf("%d\n", a + b);
return a + b;
}
int main(void) {
int (*p)(int, int);
p = Add; // Assign a value to it
Add(2, 3);
(*p)(2, 3); //p=Add,() Represents a dereference , Then why do we have * Well ?
(p)(2,3);
(**p)(2, 3);
(***p)(2, 3); // there * It's just a decoration
printf("%d", (*p)(2, 3)); // The program will print twice in this step 5
}
3 Two interesting function pointers
No.1
(*(void(*)())0)()
// First look at void(*)() This is a function pointer type
// Yes 0 Cast (void(*)())0
// You may wonder ,0 Can I also cast types ? As I said before, data is in memory , It's space + The way ( The rules )
// That is, the type , So type conversion is in the computer unless otherwise specified , Otherwise, it would be “ mandatory ”.
// This time is equivalent to defining a point 0 Array pointer variable of (void(*)())0 Here we use p Show me
// Replace it ,(*p)(), Call this function .
It can also be written like this
void(*p)();
p = 0;
(*p)(); // take p as (void(*)())0 that will do
No.2
void(*signal(int, void(*)(int)))(int);
// First we see signal,() Priority is greater than *, This is a function
// Parameters are integer and function pointer types , No variable is known as a function declaration
// What about the return value ? Let's look at the function of normal point
int Add(int , int );
int // Add the function name and () What is left is the type of return value
// take signal(int,void(*)(int)) Get rid of
// be left over void(*)(int), Is the type of return value : Function pointer type
, describe :
//signal Is a function description
//signal There are two arguments to the function , The first is int. The second is function pointers , The function pointer points to a function whose argument is int, The return type is void
//signal The return type of a function is a function pointer , The function pointer points to a function whose argument is int, The return type is void
void(*)(int) signal (int, void(*)(int));
// It's understandable , But it can't be written like this
No.2 Simplification of ——typedef
We know typedef You can add names to types with complex names , Here you can also use the function pointer type name
typedef void(*)(int) fun_p; // Normally it is written like this , But function pointers are a little special
typedef void(*fun_p)(int); // It should be written like this
fun_p signal(int, fun_p); // Function declarations can be simplified
边栏推荐
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together
- C language, to achieve three chess games
- Blue Bridge Cup Eliminate last one (bit operation, code completion)
- 《Just because》阅读感受
- Record the functions of sharing web pages on wechat, QQ and Weibo
- Scrcpy this software solves the problem of sharing mobile screen with colleagues | community essay solicitation
- PHP微信抢红包的算法
- Market Research - current situation and future development trend of marine clutch Market
- Secondary development of ANSYS APDL: post processing uses command flow to analyze the result file
猜你喜欢
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
地理探测器原理介绍
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)
[shutter] shutter gesture interaction (small ball following the movement of fingers)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
kubernetes资源对象介绍及常用命令(四)
[shutter] shutter page Jump (route | navigator | page close)
[shutter] shutter custom fonts (download TTF fonts | pubspec.yaml configure font resources | synchronize resources | globally apply fonts | locally apply fonts)
LightGBM原理及天文数据中的应用
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
随机推荐
From "bronze" to "King", there are three secrets of enterprise digitalization
kubernetes资源对象介绍及常用命令(四)
[sword finger offer] 56 - I. the number of numbers in the array
: last child does not take effect
100 important knowledge points that SQL must master: management transaction processing
New feature of go1.18: introduce new netip Network Library
Etcd Raft 协议
一周生活
The book "new programmer 002" is officially on the market! From "new database era" to "software defined car"
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
An overview of the development of affective computing and understanding research
[shutter] shutter layout component (wrap component | expanded component)
Daily book - low code you must understand in the era of digital transformation
【剑指 Offer】56 - I. 数组中数字出现的次数
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
Lightgbm principle and its application in astronomical data
Ransack combined condition search implementation
Attack and defense world PWN question: Echo
[shutter] shutter page Jump (route | navigator | page close)
服务可见可观测性