当前位置:网站首页>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 边栏推荐
- PHP微信抢红包的算法
- *C language final course design * -- address book management system (complete project + source code + detailed notes)
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- [001] [arm-cortex-m3/4] internal register
- 使用 EMQX Cloud 实现物联网设备一机一密验证
- Chargement de l'image pyqt après décodage et codage de l'image
- Unity3D学习笔记4——创建Mesh高级接口
- Image segmentation using pixellib
- Necessary browser plug-ins for network security engineers
- pyqt圖片解碼 編碼後加載圖片
猜你喜欢

LightGBM原理及天文数据中的应用

Evolution of messaging and streaming systems under the native tide of open source cloud

图像基础概念与YUV/RGB深入理解

"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba

Ransack combined condition search implementation
![[shutter] shutter application theme (themedata | dynamic modification theme)](/img/77/6b0082368943aee7108ac550141f28.gif)
[shutter] shutter application theme (themedata | dynamic modification theme)

Off chip ADC commissioning record
![[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology](/img/2f/bb99836bb3ad725483f30531ff4d53.jpg)
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology

Redis distributed lock failure, I can't help but want to burst

Lightgbm principle and its application in astronomical data
随机推荐
Market Research - current market situation and future development trend of high tibial osteotomy plate
Ransack组合条件搜索实现
Technological Entrepreneurship: failure is not success, but reflection is
Market Research - current situation and future development trend of anti-counterfeiting label market
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
一周生活
Evolution of messaging and streaming systems under the native tide of open source cloud
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks!
How to write a good program when a big book speaks every day?
Les trois principaux points de douleur traités par servicemesh
VictoriaMetrics 简介
Market Research - current market situation and future development trend of aircraft wireless intercom system
[sword finger offer] 56 - I. the number of numbers in the array
[shutter] shutter resource file use (import resource pictures | use image resources)
Oriental Aesthetics and software design
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
ArrayList analysis 2: pits in ITR, listiterator, and sublist
[001] [arm-cortex-m3/4] internal register
Share how to make professional hand drawn electronic maps
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)