当前位置:网站首页>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


版权声明
本文为[Green shirt 101060]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230401321988.html
边栏推荐
- Recommended scheme of national manufactured electronic components
- [paper reading] [3D object detection] voxel transformer for 3D object detection
- Eksctl deploying AWS eks
- Effects of antibiotics on microbiome and human health
- Supplément: annotation
- mysql table 中增加列的SQL语句
- 华为机试--高精度整数加法
- leetcode006--查找字符串数组中的最长公共前缀
- MYSQL去重方法汇总
- Understand the gut organ axis, good gut and good health
猜你喜欢
Effects of antibiotics on microbiome and human health
MYSQL50道基础练习题
How to regulate intestinal flora? Introduction to common natural substances, probiotics and prebiotics
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
Installation of zynq platform cross compiler
递归调用--排列的穷举
[paper reading] [3D target detection] point transformer
Go reflection rule
无线键盘全国产化电子元件推荐方案
Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
随机推荐
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
针对NFT的网络钓鱼
电钻、电锤、电镐的区别
[paper reading] [3D object detection] voxel transformer for 3D object detection
zynq平台交叉编译器的安装
Chlamydia infection -- causes, symptoms, treatment and Prevention
Supplément: annotation
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Luogu p1858 [multi person knapsack] (knapsack seeking the top k optimal solution)
win10, mysql-8.0.26-winx64. Zip installation
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
mysql table 中增加列的SQL语句
The perfect combination of collaborative process and multi process
520. Detect capital letters
指纹Key全国产化电子元件推荐方案
Supplement 14: cmake practice project notes (to be continued 4 / 22)
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
【论文阅读】【3d目标检测】point transformer
2020 is coming to an end, special and unforgettable.
MySQL queries users logged in for at least N consecutive days