当前位置:网站首页>C language: Advanced pointer
C language: Advanced pointer
2022-04-23 04:40:00 【Green shirt 101060】
Catalog
4. Array parameter passing and pointer parameter passing
7. A pointer to an array of function pointers

Advance of pointer
Preface
In the simple pointer above, we introduced some common uses of simple pointers
This issue , Let's learn the pointer usage of the advanced version .
1. Character pointer

among *p It doesn't point to “abcdef” Put the pointer p in , Instead, the address of the first character is placed in the pointer p in . This is a mistake that many people often make .

Because of this nature , When printing a single character , The pointer needs to be dereferenced , While using %s When printing the whole string , Just pass the first element address , The pointer just points to the address of the first element .
Once an interview example :

The answer is :......not same .......same
reason : The first two character arrays , Is to put the characters in str1、str2 in , The two addresses are different . So they are different . And the pointer str3、 The pointer str4 It refers to the string stored in the same memory , So they are the same .
2. Array pointer
seeing the name of a thing one thinks of its function , This is a pointer , And it's a pointer to an array .
So what kind of form is he ? We know
int* arr[10];because [ ] The priority of is higher than * You have to be tall , therefore , Array names are precedence and [ ] The combination of , This is the pointer array .
therefore , We should make sure that * And array name first , Form the following :
int (*arr)[10];
& The difference between array name and array name
We know that the array name represents the address of the first element of the array , that & What does the array name represent ? Let's look at this code :
#include <stdio.h> int main() { int arr[10] = {0}; printf("%p\n", arr); printf("%p\n", &arr); return 0; }The operation results are as follows :
You can see that the result is the same , But is it really the same ?
#include <stdio.h> int main() { int arr[10] = { 0 }; printf("arr = %p\n", arr); printf("&arr= %p\n", &arr); printf("arr+1 = %p\n", arr+1); printf("&arr+1= %p\n", &arr+1); return 0; }The result of this code is obvious
actually :& The array name represents the address of the whole array , When the address of the array +1 When , Skip the size of the entire array . So the difference is 40 了
Array pointers are applied to instances of two-dimensional arrays

3. Pointer array
We have already talked about , Pointer array is an array of pointers .
4. Array parameter passing and pointer parameter passing
When the array name is passed in , Function arguments, whether array 、 The pointer 、 Or a pointer [ ] It's all right
When you pass in the array name of the pointer array , Then we need to use the secondary pointer , Point to the address of the pointer .
Two dimensional array :

When passing parameters with a two-dimensional array , The same as the previous initialization , The number of columns must be given , Otherwise, it's wrong .
and int *arr [ ] This is a pointer error , The elements inside are pointers , So it must be wrong .
int *arr The error is because the first element of a two-dimensional array is a row , You can't receive a line with a pointer .
Receiving with a secondary pointer must also be , FALSE , Because it's the address of the element , And elements are shaping .
5. A function pointer
A function pointer is a pointer used to store the address of a function .
In the form of : return type +(* Array name )( Function parameter type )
void (*pf)(int , int);Why use () Give Way * Combine... With function name first ? The principle is the same as that of array pointers , because () Higher priority , Will preferentially combine with parameters , So it becomes a function .
It doesn't matter whether the name of the parameter is written or not .


In use , This * It's optional , The two lines with horizontal lines in the picture have the same effect . Even you can use ****** It's all right .
6. Function pointer array
An array is full of function pointers , Then this array is the function pointer array .

The code above shows .
Why do we have to [ ] Put it in parentheses ? Because its nature is an array , So the name is to be with [ ] Combined first .
7. A pointer to an array of function pointers

The pointer to the bottom row of the array is the pointer to the function .
How to understand this industry ? Let's dismantle him first
First of all, he is a (*ppfarr), Then his nature is a pointer , Then outside is [ ] Second, his property is an array , Then talk to the outside * combination , So we can understand it as , This is a pointer , Point to an array , Stored in the array is the function pointer .
8. Callback function
And here's int_cmp A function is a callback function , The following qsort A function calls it by using its function pointer .
版权声明
本文为[Green shirt 101060]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230401321988.html
边栏推荐
- Huawei machine test -- high precision integer addition
- [AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
- 递归调用--排列的穷举
- Recommended scheme of national manufactured electronic components for intelligent electronic scales
- Chlamydia infection -- causes, symptoms, treatment and Prevention
- A lifetime of needs, team collaboration can play this way on cloud nailing applet
- 智能电子秤全国产化电子元件推荐方案
- Error occurs when thymeleaf th: value is null
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
- leetcode005--原地删除数组中的重复元素
猜你喜欢

zynq平台交叉编译器的安装

Record your own dataset with d435i, run orbslam2 and build a dense point cloud

兼容NSR20F30NXT5G的小体积肖特基二极管

The perfect combination of collaborative process and multi process

补充番外14:cmake实践项目笔记(未完待续4/22)

阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
![[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022](/img/c2/fd9531e48118e384bc5d490ee8e506.png)
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022

QML advanced (IV) - drawing custom controls

The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video

协程与多进程的完美结合
随机推荐
Leetcode - > 1 sum of two numbers
mysql ,binlog 日志查询
IEEE Transactions on Industrial Informatics(TII)投稿须知
Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
Apache Bench(ab 压力测试工具)的安装与使用
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
无线键盘全国产化电子元件推荐方案
A new method for evaluating the quality of metagenome assembly - magista
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
Recursive call -- Enumeration of permutations
Recommended scheme of national manufactured electronic components for intelligent electronic scales
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
io. Platform. packageRoot; // ignore: deprecated_ Member_ use
Logger and zap log Library in go language
Inverse system of RC low pass filter
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
从MySQL数据库迁移到AWS DynamoDB
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
Huawei machine test -- high precision integer addition
Recommended scheme for national production of electronic components of wireless keyboard

