当前位置:网站首页>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
边栏推荐
- Effects of antibiotics on microbiome and human health
- MYSQL查询至少连续n天登录的用户
- leetcode009--用二分查找在数组中搜索目标值
- Error occurs when thymeleaf th: value is null
- Recursive call -- Enumeration of permutations
- io. Platform. packageRoot; // ignore: deprecated_ Member_ use
- shell wc (统计字符数量)的基本使用
- SQL statement for adding columns in MySQL table
- eksctl 部署AWS EKS
- 補:注解(Annotation)
猜你喜欢

Phishing for NFT

Recommended scheme of national manufactured electronic components

针对NFT的网络钓鱼

520.检测大写字母

【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估

洛谷P1858 【多人背包】 (背包求前k优解)

Recommended scheme for national production of electronic components of wireless keyboard

Supplement: Annotation

383. Ransom letter

做数据可视化应该避免的8个误区
随机推荐
Basic operation of sequence table
What is the thirty-six plan
在AWS控制台创建VPC(无图版)
shell wc (统计字符数量)的基本使用
Coinbase:关于跨链桥的基础知识、事实和统计数据
Error occurs when thymeleaf th: value is null
Mysql---数据读写分离、多实例
Understand the gut organ axis, good gut and good health
协程与多进程的完美结合
leetcode002--将有符号整数的数字部分反转
[paper reading] [3D target detection] point transformer
Interaction of diet gut microbiota on cardiovascular disease
程序员抱怨:1万2的工资我真的活不下去了,网友:我3千咋说
Redis 命令大全
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Recommended scheme of national manufactured electronic components for intelligent electronic scales
leetcode006--查找字符串数组中的最长公共前缀
FAQ of foreign lead and alliance Manager
三十六计是什么

