当前位置:网站首页>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;
}边栏推荐
- MySQL Part 4 (end)
- Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
- 谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
- Alibaba Font Icon Library Usage and update methods
- Let variable declaration feature of ES6 new feature and its case
- 路西法98-生活记录ing
- Extended operator of new features in ES6
- 当我从数据库获取到了winfrom特定的控件ID之后我需要通过这个ID找到对应的控件,并对控件的TEXT文本进行赋值这该怎么做
- 新零售O2O 电商模式解析
- Typescript from getting started to mastering (XXIII) namespace namespace (Part 2)
猜你喜欢

Const char* and char*, string constants

The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!

Three tier architecture of enterprise network

Simple cases of inner connection and left connection

企业网的三层架构

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

3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘

SSL==证书相关概念

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

Basic configuration of BGP - establish peers and route announcements
随机推荐
Typescript from getting started to mastering (XVI) configuration file - first knowledge of compileroptions configuration item
Microcomputer principle and interface technology
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
Beijing post network research 2015 problem2
The list is not updated in real time when JS V-for data changes
Alibaba Font Icon Library Usage and update methods
Deconstruction assignment of new features of ES6
一文学透MySQL表的创建和约束
谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
3.解决Pycharm报错Unresolved reference ‘selenium‘ Unresolved reference ‘webdriver‘
The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
STM32F103ZET6程序移植为C8T6+C8T6下载程序flash timeout的解决方案
Uni app internationalization
企业网的三层架构
C declaration and initialization and assignment
Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation
数据挖掘——关联分析例题代码实现(下)
First knowledge of C language (3)
Lucifer 98 life record ing
flink-sql 如何设置 sql执行超时时间