当前位置:网站首页>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 边栏推荐
- #include<>和#include“”的区别
- ServiceMesh主要解决的三大痛点
- B.Odd Swap Sort(Codeforces Round #771 (Div. 2))
- Les trois principaux points de douleur traités par servicemesh
- 基于ASP.net的手机销售管理系统(二手手机销售管理系统)+ASP.NET+C#语言+VS2010+数据库可以用于课设、毕设学习
- Market Research - current market situation and future development trend of third-party data platform
- [001] [arm-cortex-m3/4] internal register
- Market Research - current situation and future development trend of environmental friendly fireworks Market
- APP页面分享口令Rails实现
- Landingsite eband B1 smoke test case
猜你喜欢

Official announcement! The golden decade of new programmers and developers was officially released

pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform

New feature of go1.18: trylock, which has been tossed n times
![[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)](/img/fa/5c1b6c16d9aabd13e9a4f7c7b9c7da.jpg)
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)

How to prevent your jar from being decompiled?

The difference between include < > and include ""

Introduction to the principle of geographical detector

"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!

Off chip ADC commissioning record

The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
随机推荐
APP页面分享口令Rails实现
Market Research - current situation and future development trend of anti-counterfeiting label market
在beforeDestroy中销毁localStorage中的值无效
[leetcode] sword finger offer 11 Rotate the minimum number of the array
Record the functions of sharing web pages on wechat, QQ and Weibo
Web侧防御指南
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Introduction to the principle of geographical detector
Market Research - current situation and future development trend of marine clutch Market
【剑指 Offer 】56 - II. 数组中数字出现的次数 II
[001] [arm-cortex-m3/4] internal register
Five message formats of OSPF
Market Research - current market situation and future development trend of third-party data platform
Les trois principaux points de douleur traités par servicemesh
[shutter] shutter resource file use (import resource pictures | use image resources)
Leetcode theme [array] -169- most elements
Bridge emqx cloud data to AWS IOT through the public network
540. Single element in ordered array
Daily book - low code you must understand in the era of digital transformation