当前位置:网站首页>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
边栏推荐
- Reading experience of just because
- Market Research - current situation and future development trend of carob chocolate market
- CVPR论文解读 | 弱监督的高保真服饰模特生成
- 分享一下如何制作专业的手绘电子地图
- Market Research - current market situation and future development trend of marine wet exhaust hose
- PIP audit: a powerful security vulnerability scanning tool
- Technical solution of vision and manipulator calibration system
- 【C 题集】of Ⅴ
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- sql service 截取字符串
猜你喜欢
CVPR论文解读 | 弱监督的高保真服饰模特生成
Reading experience of just because
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
C语言,实现三子棋小游戏
*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
Evolution of messaging and streaming systems under the native tide of open source cloud
#include<>和#include“”的区别
Daily book - low code you must understand in the era of digital transformation
Error in PIP installation WHL file: error: is not a supported wheel on this platform
随机推荐
情感计算与理解研究发展概述
Pip install whl file Error: Error: … Ce n'est pas une roue supportée sur cette plateforme
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
ArrayList analysis 2: pits in ITR, listiterator, and sublist
Servicemesh mainly solves three pain points
Leetcode theme [array] -169- most elements
: last child does not take effect
[zero foundation I] Navicat download link
Market Research - current situation and future development trend of cell-based seafood market
[001] [arm-cortex-m3/4] internal register
PIP audit: a powerful security vulnerability scanning tool
Market Research - current situation and future development trend of environmental friendly fireworks Market
基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
Infrastructure is code: a change is coming
[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)
分享一下如何制作专业的手绘电子地图
LightGBM原理及天文数据中的应用
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
How to prevent your jar from being decompiled?
20220702 how do programmers build knowledge systems?