当前位置:网站首页>Pointer of pointer???...
Pointer of pointer???...
2022-07-29 04:00:00 【Xiaowa 123】
The pointer of the pointer is really troubled for a long time , What exactly is the pointer used for ?
Need to know a place : Pointer is also a variable . Let's take a look at an example of applying secondary pointers .
PS: add ,int* p,p Is the name of a pointer variable .
Purpose : I hope to apply for a piece of dynamic memory in the heap to store the structure through sub functions Data, And modify the value of structure members .
#include <stdio.h>
#include <stdlib.h>
typedef struct Data
{
int da;
} Data;
void fun(Data* p);
int main()
{
Data* d;
fun(d);
A pointer variable is passed in here , Or is it equivalent to value passing , What should be passed is the address of this pointer variable .
printf("%d", d->da);
return 0;
}
void fun(Data* p)
The purpose of this function is to create a pointer p, Apply for a piece of dynamic memory in the heap to store the structure Data,
And modify the value of the structure member through the pointer .
{
p = (Data*)malloc(sizeof(Data));
p->da = 2;
} It's useless to do this . Why? ? We hope to directly change the value of the structure stored in the heap through pointers ,
When a function is called fun() when , For Data* d As a function fun() Input , Return to p Is the address of a dynamic memory allocated in the stack , But be careful :Data* p Is a pointer variable , But it is also a local variable , call fun() Function time , The essence is value passing , Not address passing , With fun() The call to the function ends , The pointer p This local variable disappears .
If you want to allocate a piece of space in the heap , And I also want to modify its value by pointer , You can select the pointer of the pointer .
Look at the following code :
#include <stdio.h>
#include <stdlib.h>
typedef struct Data
{
int da;
} Data;
void fun(Data**p); // It's changed here
int main()
{
Data* d;//d It's a pointer variable
fun(&d); // Here, the input parameter of the function should be the pointer of the pointer , That is to say d The address of , The address of the pointer
printf("%d", d->da);
if (d)
free(d);
return 0;
}
void fun(Data**p) // Want to change the value of the pointer , You should use the address of the pointer , You want the address stored in the pointer variable to change , This parameter means *p It's a first level pointer variable
{
(*p) = (Data*)malloc(sizeof(Data));
(*p)->da = 2; // It's changed here
}If you look carefully, it is not difficult to find that the changed code uses pointers to pointers
In fact, the purpose of this program is to dynamically allocate the address to a pointer variable , Dynamically return an address . According to the initial writing , The question is whether this or value passing , Not address passing , Although the parameter is a pointer variable , But it's not the delivery address .
Why can't we use pointers directly ?
Because in the first code void fun(Data *p) The middle parameter is press Value passed . The calling function automatically defines a temporary variable to store a copy of the argument , So in fun Dynamically allocated memory in , It's not at all main Pointer variable in function d The point is ,main Middle pointer variable d It is undefined from beginning to end .
!!!!!!!!!!!!!!!! So much for that , In fact, it is still not clear what the secondary pointer is used for , Let's take a look at this article , I hope you understand .
https://blog.csdn.net/majianfei1023/article/details/46629065
If p Is a pointer to an integer variable b The pointer to ,a It's pointing p Second level pointer of , that **a=*(*a)=*p=int b.
Note that pointer variables are not always carried * Of , People also have independent variable names . I don't mean to carry it all the time * Of ,* Only when defining this pointer variable, it indicates that it is a pointer variable .
In short , When to use the secondary pointer , When you want to change the value of a pointer variable in a function , Use secondary pointers as function parameters , That is to pass in the address of the pointer , Change the value of the pointer , That is to change the address stored in the first level pointer . If the function parameter is a first-order pointer , If a first-order pointer is also defined outside the function , Or is it equivalent to value passing , The value of the modified variable is not passed through the address . By passing the address, you can change the variable pointed to by the pointer inside the function .
At the bottom is a program written by myself , Let's run it
#include<stdio.h>
void my_malloc(int* *m)//*m Is a pointer variable , Hope to let *m To receive malloc Return address , So it is (*m)=mallocXXX
{
(*m) = (int*)malloc(100);
}
void errmalloc(int* q)
{
q = (int*)malloc(100);
}
int main(void)
{
int a = 5;
int* p = &a;
printf(" The pointer p The value of is %p\n", p);
errmalloc(p);
printf(" The pointer p The value of is %p\n", p);
my_malloc(&p);
printf(" The pointer p The value of is %p\n", p);
return 0;
}边栏推荐
- Arrow function of new features of ES6
- [introduction to C language] zzulioj 1031-1035
- Cannot paste multiple pictures at once
- UCOS task switching process
- Why do many programmers hate pair programming?
- Li Kou daily question - day 44 -205. Isomorphic string
- Flutter 启动白屏
- [principle] several ways of horizontal penetration
- I. creation and constraint of MySQL table
- Why do programmers so "dislike" the trunk development mode?
猜你喜欢

Interview essential! TCP classic 15 consecutive questions!

Connection broken by 'readtimc rt-443): read timed out (read timeout=l5)“)‘: /pac

消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!

HCIP BGP

Summary on the thought of double pointer

1. Mx6u driver development-2-led driver

安装ros的laser_scan_matche库所遇到的问题(一)

Typescript from getting started to mastering (XXII) namespace namespace (I)

First knowledge of C language (3)

@Configuration (proxybeanmethods = false) what's the use of setting this to false
随机推荐
Wechat applet monitors sliding events on the screen
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
Form verification of landline
Nacos registry
What have I learned from 200 machine learning tools?
mmdetection初步使用
Lucifer 98 life record ing
Note: restframe work records many to one tables, how to serialize in that table (reverse query)
Batch production and upload sales NFT opensea eth polygon
HCIP BGP
EMD 经验模态分解
JS realizes the function of one click Copy
STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案
Sunflower senior product director technology sharing: "how to apply national remote control" in AD domain environment
Solve the delay in opening the console of Google browser
How fast does it take to implement a super simple language
C语言实现三子棋游戏(详解)
Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理
Data too long for column 'xxx' at row 1 solution
LDP -- label distribution protocol