当前位置:网站首页>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; } |
边栏推荐
- 关于项目采购管理,这些你需要知道
- Chapter 6 picture operation of canvas
- JS 会有变量提升和函数提升
- 如果我在佛山,到哪里开户比较好?究竟网上开户是否安全么?
- 模电 2个NPN管组成的恒流源电路分析
- CKS CKA CKAD 将终端更改为远程桌面
- What should phpcms do when it sends an upgrade request to the official website when it opens the background home page?
- synchronized 与多线程的哪些关系
- Heavyweight! The latest SCI impact factors were released in 2022, and the ranking of the three famous journals NCS and the top10 of domestic journals has changed (the latest impact factors in 2022 are
- 高並發軟件(網站,服務器端接口)的評價指標
猜你喜欢

EMC-浪涌防护及退耦设计

Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear

信息学奥赛一本通1194:移动路线

Swagger2的配置教程

ModStartBlog 现代化个人博客系统 v5.2.0 主题开发增强,新增联系方式

Huali biology rushes to the scientific innovation board: the annual revenue is RMB 226million and it is planned to raise RMB 800million

第五届中国软件开源创新大赛 | openGauss赛道直播培训

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

VQA needs not only pictures, but also external knowledge! University of Washington & Microsoft proposed revive, using gpt-3 and wikidata to help answer questions

mysql 备份与还原
随机推荐
symfony框架安全组件(security)防火墙配置
Jet hydrogen technology rushes to the scientific innovation board: SAIC Group is the major shareholder to raise 1.06 billion yuan
[QT tutorial] QPushButton key and double click effect
EMC-浪涌防护及退耦设计
Huali biology rushes to the scientific innovation board: the annual revenue is RMB 226million and it is planned to raise RMB 800million
MCS:离散随机变量——几何分布
China soft ice cream market forecast and investment prospect research report (2022 Edition)
威高血液净化冲刺香港:年营收29亿 净利降12.7%
Practical application cases of drives
宜明昂科冲刺港股:年内亏损7.3亿 礼来与阳光人寿是股东
CKS CKA CKAD 将终端更改为远程桌面
Source code of campus secondary market
网易严选离线数仓质量建设实践
MCS:离散随机变量——Hyper Geometric分布
他山之石 | 丁香园 医疗领域图谱的构建与应用
The first lesson on cloud - how easy is it to build a small broken station? The old driver of cloud computing will take you one hour to finish it
MCS:多元随机变量——离散随机变量
Configuration tutorial for swagger2
我登录RDB的数据库,提示需要主账号授权,这个我怎么知道该找谁?
What should phpcms do when it sends an upgrade request to the official website when it opens the background home page?