当前位置:网站首页>Secondary pointer
Secondary pointer
2022-06-29 14:58:00 【Soy sauce;】
1. Second level pointer basic concept ( Data type of pointer variable address )
Let's take a moment to look at an example , Open the prelude to this upcoming . Consider the following statements :
int a = 12; int *b = &a; |
They allocate memory as shown in the figure below :

Suppose we have a second 3 A variable , named c, And initialize it with the following statement :
c = &b; |
Its general appearance in memory is as follows :

The problem is :c What is the type of ? Obviously it's a pointer , But what does it point to ? Variable b It's a “ Pointer to an integer ”, So anything that points to b The type of must be pointing to “ Pointer to an integer ” The pointer to , More colloquially , Is a pointer to a pointer .
Is it legal ? Yes ! Pointer variables are the same as other variables , Occupy a specific location in memory , So use & It is legal for the operator to get its address .
So how is this variable declared ?
int **c = &b; |
So this **c How to understand that ?* The operator has the combination of thinking from the right , So this expression is equivalent to *(*c), We apply for jobs layer by layer from the inside out .*c visit c The point is , We know it's a variable b. The second indirect access operator accesses the address that this location points to , That's the variable a. The pointer of a pointer is not hard to understand , Just watch out for all the arrows , If the indirect access operator appears in the expression , You're going to follow the arrow to where it's pointing .

2. The second level pointer is used as the formal parameter output feature
Explain :a[2] and *a+2 Same
The output characteristic of the secondary pointer as a parameter refers to the allocation of memory by the called function , The main function uses .
// Modulated function , By the parameter n Determine how many elements of memory are allocatedvoid allocate_space(int **arr,int n){ // Heap allocation n individual int Type element memory int *temp = (int *)malloc(sizeof(int)* n); if (NULL == temp){ return; } // Initialize the memory int *pTemp = temp; for (int i = 0; i < n;i ++){ //temp[i] = i + 100; *pTemp = i + 100; pTemp++; } // Pointer indirect assignment *arr = temp; } // Print array void print_array(int *arr,int n){ for (int i = 0; i < n;i ++){ printf("%d ",arr[i]); } printf("\n"); } // Secondary pointer output characteristics ( Allocate memory by the called function )The main functionvoid test(){ int *arr = NULL; int n = 10; // to arr Pointer indirect assignment allocate_space(&arr,n); // Output arr Points to the memory of the array print_array(arr, n); // Release arr The value of the memory space pointed to if (arr != NULL){ free(arr); arr = NULL; } } |
3. The secondary pointer is used as a formal parameter input feature
The second level pointer is used as formal parameter input, which means that the main calling function allocates memory , The called function uses it
// Print array void print_array(int **arr,int n){ for (int i = 0; i < n;i ++){ printf("%d ",*(arr[i])); } printf("\n"); } // Secondary pointer input characteristics ( Allocate memory by the calling function ) void test(){ The stack area applies for data memory int a1 = 10; int a2 = 20; int a3 = 30; int a4 = 40; int a5 = 50; int n = 5; int** arr = (int **)malloc(sizeof(int *) * n); arr[0] = &a1; arr[1] = &a2; arr[2] = &a3; arr[3] = &a4; arr[4] = &a5; Print heap data print_array(arr,n); Release the heap area free(arr); arr = NULL; } |
边栏推荐
猜你喜欢

【牛客网刷题系列 之 Verilog快速入门】~ 移位运算与乘法

EMC-浪涌防护及退耦设计

Chapter 12 other applications of canvas

Jet hydrogen technology rushes to the scientific innovation board: SAIC Group is the major shareholder to raise 1.06 billion yuan

SOFARegistry 源码|数据同步模块解析

June 27 talk SofiE

云上第一课 | 建个小破站有多简单?云计算老司机带你一小时搞定

redis在window和Linux环境下的安装

论文学习——考虑场次降雨年际变化特征的年径流总量控制率准确核算

idea输出台输出中文乱码问题
随机推荐
Informatics Olympiad all in one 1003: aligned output
模电 2个NPN管组成的恒流源电路分析
Alibaba cloud experience Award: use polardb-x and Flink to build a large real-time data screen
JS 会有变量提升和函数提升
Differences between @resource and @autowired annotations automatically injected:
Weigao blood purification sprint to Hong Kong: annual revenue of RMB 2.9 billion, net profit decreased by 12.7%
Chapter 6 picture operation of canvas
MCS:多元随机变量——离散随机变量
Draw a slash on a plane coordinate
重磅!2022最新SCI影响因子发布,三大名刊NCS及国内期刊TOP10排名有变化 (内附2022年最新影响因子)
信息学奥赛一本通1002:输出第二个整数
阿里云体验有奖:使用PolarDB-X与Flink搭建实时数据大屏
技术沟通遇到3个为什么背后的逻辑
[practical chapter of correlation analysis] why can't Bi software do correlation analysis
symfony框架安全组件(security)防火墙配置
高并发软件(网站,服务器端接口)的评价指标
Huali biology rushes to the scientific innovation board: the annual revenue is RMB 226million and it is planned to raise RMB 800million
论文学习——考虑场次降雨年际变化特征的年径流总量控制率准确核算
信息学奥赛一本通1000:入门测试题目
信息学奥赛一本通2062:电影票