当前位置:网站首页>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; } |
边栏推荐
猜你喜欢

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

Redis installation in windows and Linux Environment

curl: (56) Recv failure: Connection reset by peer

ROS 笔记(10)— launch 文件启动

Analysis of constant current source circuit composed of two NPN tubes

第九章 APP项目测试(此章完结)

宜明昂科冲刺港股:年内亏损7.3亿 礼来与阳光人寿是股东

Query function of Excel vlookup

又拍云 Redis 的改进之路

Whitelabel error page access
随机推荐
Practical application cases of drives
Opengauss community establishes sig knowledgegraph
Informatics Olympiad all in one 2062: movie tickets
ModStartBlog 现代化个人博客系统 v5.2.0 主题开发增强,新增联系方式
openGauss社区成立SIG KnowledgeGraph
MCS:多元随机变量——多项式分布
JS 会有变量提升和函数提升
Chinese garbled code output from idea output station
Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
Mysql database - general syntax DDL DML DQL DCL
ROS notes (10) - Launch file startup
墨滴排版
I want to search the hundreds of nodes in the data warehouse. Can I check a table used in the SQL
If I am in Foshan, where can I open an account? Is it safe to open an account online?
Ogg synchronize MySQL data to greenplus
phpcms打开后台首页时向官网发送升级请求觉得卡怎么办?
PostgreSQL learning (based on rookie course)
EXCEL的查询功能Vlookup
高并发软件(网站,服务器端接口)的评价指标
Chapter 13 event operation of canvas