当前位置:网站首页>Change the value of the argument by address through malloc and pointer
Change the value of the argument by address through malloc and pointer
2022-07-29 04:02:00 【Xiaowa 123】
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void getMem(int* p)
{
p=(int*)malloc(sizeof(int));
*p=5;
}
int main(void)
{
int a=1;
int* p1=&a;
getMem(p1);
printf("a=%d\n",a);
return 0;
}
The original meaning of the code is : Defining pointer variables p1,p1 Point to main Variables in functions a, By calling getMem Function to variable a Change arguments by addressing a Value , Change it to 5, But the output result is 1, Explanatory variable a The value of . The reason lies in the wrong use of malloc.
【 change 】:
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void getMem(int* p)
{
// p=(int*)malloc(sizeof(int));
You need to block the above sentence , Because after passing in the parameters here , What comes in is main Function a The address of , But this statement makes the pointer variable p It points to a newly opened space ,p No longer point to a 了 , So below *p=5 It only plays a role in malloc The memory space is assigned as 5, But this time p Stored in is no longer a The address of the .
*p=5;
}
int main(void)
{
int a=1;
int* p1=&a;
getMem(p1);
printf("a=%d\n",a);
return 0;
}Let's do another example , It involves address and value transmission , as well as malloc, The secondary pointer
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
void getMemory(char* *p)
{
*p=(char*)malloc(15);
}
int main(void)
{
char* str =NULL;
getMemory(&str);
Change the argument by calling a function that does not return a value str Value , Should be str Pointer to as a function parameter , This function parameter must be of pointer type ( Several levels of pointer are still ignored )
By calling a function that does not return a value , You need to change the value of the argument , Address should be used .
because str It's a pointer in itself , therefore *str It's a secondary pointer .
strcpy(str,"hello");
printf("%s",str);
return 0;
}边栏推荐
- 【redis系列】字符串数据结构
- The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
- 【BGP】小型实验
- SQL语句 关于字段转换怎么写
- Since 2019, you must have stopped using this marketing strategy
- C language - character array - string array - '\0' -sizeof-strlen() -printf()
- Analysis of new retail o2o e-commerce model
- Solve the problem of garbled code when opening the project code in idea
- 数据源是SQL server ,我要配置日期字段 updateDate 最后两天日期的增量数据,做增
- Data too long for column 'xxx' at row 1 solution
猜你喜欢

LDP --- 标签分发协议

Malloc C language

【BGP】小型实验

关于双指针的思想总结

Design of environment detection system based on STM32 and Alibaba cloud

【深度学习CPU(番外篇)——虚拟内存】

Deep understanding of browser caching mechanism (HTTP)

STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案

Ssl== certificate related concepts

力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
随机推荐
mmdetection初步使用
Install the laser of ROS_ scan_ Problems encountered in match library (I)
Design of environment detection system based on STM32 and Alibaba cloud
1985-2020 (8 Editions) global surface coverage download and introduction
Ribbon principle analysis namedcontextfactory
Simple cases of inner connection and left connection
Analysis of new retail o2o e-commerce model
About the writing of ALV format control part
MySQL第四篇(完结)
MySQL第三篇
Is the browser multi process or single process?
C language - character array - string array - '\0' -sizeof-strlen() -printf()
With more than 5 years of work experience and a salary of 15K, would you accept it if you were me?
Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
Data mining -- code implementation of association analysis example (Part 2)
How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency
小马智行进军前装量产,从自研域控制器入手?
EMD empirical mode decomposition
C语言实现三子棋游戏(详解)
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?