当前位置:网站首页>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;
}边栏推荐
- How to write SQL statements about field conversion
- UCOS任务切换过程
- BGP的基础配置---建立对等体、路由宣告
- Extended operator of new features in ES6
- Typescript from getting started to mastering (XXII) namespace namespace (I)
- 1985-2020(8个版次)全球地表覆盖下载与介绍
- Shopify seller: EDM marketing should be combined with salesmartly to easily get the conversion rate
- Data too long for column 'xxx' at row 1 solution
- Malloc C language
- 代码 ~ 隐藏或禁用状态栏和虚拟按键
猜你喜欢

LVS+KeepAlived高可用部署实战应用
![[deep learning CPU (part outside) - virtual memory]](/img/f7/4c72d583456f6f68c52424602ff5d9.png)
[deep learning CPU (part outside) - virtual memory]

Note: restframe work records many to one tables, how to serialize in that table (reverse query)

C语言实现三子棋游戏(详解)

关于双指针的思想总结

Alibaba Font Icon Library Usage and update methods

Analysis of new retail o2o e-commerce model

基于STM32和阿里云的环境检测系统设计

BGP的基础配置---建立对等体、路由宣告

Configmap configuration and secret encryption
随机推荐
OA项目之会议通知(查询&是否参会&反馈详情)
Common methods of lodash Library
Const char* and char*, string constants
Alibaba Font Icon Library Usage and update methods
First knowledge of C language (3)
Some notes on uniapp
Note: restframe work records many to one tables, how to serialize in that table (reverse query)
面试必备!TCP协议经典十五连问!
华为天才少年稚晖君做了一把模块化机械键盘,引起极客圈地震,网友:这才是真正的客制化...
UCOS任务切换过程
Array as function parameter -- pointer constant / constant pointer
【BGP】小型实验
LDP -- label distribution protocol
MySQL Part 4 (end)
CUB_ Visualization of key points in 200 bird dataset
Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理
STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案
Lucifer 98 life record ing
BGP的基础配置---建立对等体、路由宣告
大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?