当前位置:网站首页>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 边栏推荐
- Try to get property'num for PHP database data reading_ rows' of non-object?
- *C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
- 【剑指 Offer】56 - I. 数组中数字出现的次数
- Bridge emqx cloud data to AWS IOT through the public network
- Basic IO interface technology - microcomputer Chapter 7 Notes
- How do I access the kubernetes API?
- Market Research - current situation and future development trend of carob chocolate market
- Les trois principaux points de douleur traités par servicemesh
- An overview of the development of affective computing and understanding research
- Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
猜你喜欢

The difference between include < > and include ""

Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale

Daily book -- analyze the pain points of software automation from simple to deep
![[shutter] shutter page Jump (route | navigator | page close)](/img/af/3fb2ca18bcec23a5c0c6897570fb53.gif)
[shutter] shutter page Jump (route | navigator | page close)
![[staff] Sibelius 7.5.1 score software installation (software download | software installation)](/img/1a/4932a7931c54248c065cf8a1462d34.jpg)
[staff] Sibelius 7.5.1 score software installation (software download | software installation)

《Just because》阅读感受

Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?

From "bronze" to "King", there are three secrets of enterprise digitalization
![[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)](/img/f7/cb41d159e5c5ef3f4f1b9468d52ccc.jpg)
[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)

Introduction to the principle of geographical detector
随机推荐
Market Research - current market situation and future development trend of genome editing mutation detection kit
[zero foundation I] Navicat download link
A week's life
[staff] Sibelius 7.5.1 score software installation (software download | software installation)
[shutter] shutter application life cycle (foreground state resumed | background state paused | inactive | component separation state detached)
情感计算与理解研究发展概述
From "bronze" to "King", there are three secrets of enterprise digitalization
Unity3d learning notes 4 - create mesh advanced interface
Ransack组合条件搜索实现
[001] [arm-cortex-m3/4] internal register
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!
【零基础一】Navicat下载链接
Redis distributed lock failure, I can't help but want to burst
ServiceMesh主要解决的三大痛點
LandingSite eBand B1冒烟测试用例
Market Research - current situation and future development trend of environmental friendly fireworks Market
Daily book - low code you must understand in the era of digital transformation
Share how to make professional hand drawn electronic maps