当前位置:网站首页>详解C语言实参与形参的区别
详解C语言实参与形参的区别
2022-06-11 12:51:00 【Please trust me】
基本概念
相信大家对形参与实参两个概念已经见多不怪了,这里我们再来加深一下印象或理解。实参:所谓实参就是实际在内存中开辟了的空间的变量参数。形参:指的是函数的参数,之所以叫形参,是因为只有在函数被调用的时候才会给该变量在栈中开辟空间,并将实参拷贝给形参
案例1
这里我就直接上指针了,大家搞懂了指针,一切且懂,请看如下代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void func(char * buffer){
printf("%x\n",buffer);
if(buffer){
free(buffer);
buffer=NULL;
}
return ;
}
int main(){
char *buf=NULL;
printf("%x\n",buf);
buf =(char *)malloc(100);
if(buf==NULL){
printf("test_1\n");
}
func(buf);
if(buf==NULL){
printf("test_2");
}else{
printf("test_3");
}
return 0;
}
最后程序的打印结果是什么呢?
案例2
请看如下代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void swap(int * a,int *b)
{
int *temp;
temp = a;
a=b;
b=temp;
printf("%d %d\n",*a,*b);
return ;
}
int main(){
int a=6;
int b=10;
int *p,*q;
p=&a;
q=&b;
swap(p,q);
printf("%d %d\n",*p,*q);
return 0;
}
这段代码的输出结果又是多少呢?
答案
案例1:
答案:test_3
分析如下:
我们在堆上成果申请了100个字节大小的空间,malloc函数会返回申请成功的堆地址上的地址,所以肯定是不会打印test_1的,然后我们调用函数func,去释放形参buffer,这段在堆上的空间肯定是被释放了的。且buffer被置空了,但是关我们buf有什么关系,所以buf不为NULL,答案就是test_3
案例2
答案:10 66 10原理同案例1,抓住实参和形参的关系把握在内容拷贝就OK

边栏推荐
- After Oracle deletes a user, it can still use the user to log in
- openharmony标准系统移植之音频适配
- Development of smart contract DAPP system for TRX wave field chain
- 【bug解决】表单分页,显示总数据res.data.total
- 添加环境路径
- Clear the selected data in the modal box after the modal box is closed
- 两件小事,感受到了和大神的差距
- 石头科技:研发实力和过硬品质 助力扫地机器人产业升级
- 知麻Z1投影仪真的好用吗?实际效果怎么样?
- Add function drop-down multiple selections to display the selected personnel
猜你喜欢

【接口】看接口路径 查接口

Venue floor efficiency is so low? The key lies in these two aspects

逆向学习入门-优秀的汇编调试工具OllyDbg

游泳馆暑期业绩翻倍的方法

Schéma de dessin utilisé par les Pads

What are the elements of running a gymnasium?

4K投影仪哪款性价比最高,当贝X3 Pro高亮128G存储618值得看

4. Locksupport and thread interruption

启封easy QF PDA帮助企业提升ERP的管理水平

pip2pi和pypiserver及Apache在pip本地源配置中的应用实践
随机推荐
After Oracle deletes a user, it can still use the user to log in
Zhongfu Jinshi: with the rapid development of the intelligent bathroom industry, the intelligent toilet will usher in a highlight moment
Mctalk's entrepreneurial voice - erudition and discernment: be interested in socializing, and provide a "small and beautiful" space for old friends before and after retirement
Adobe Premiere foundation - batch material import sequence - variable speed and rewind (recall) - continuous action shot switching - subtitle requirements (13)
ProblemB. Phoenix and Beauty
ASCII, Unicode, and UTF-8
2022 vmvare download and installation tutorial on the latest official website (ultra detailed)
【bug解决】表单分页,显示总数据res.data.total
馆客多游泳馆会员管理系统可以实现哪些场景?
Record a JVM GC process
【问题总结】$t
字节真的是宇宙尽头吗?
TeaTalk·Online 演讲实录 | 圆满完结!安全上云,选对数据迁移策略很重要
换种方式实现阖家团圆,2022旗舰投影坚果J10S被种草
. The way to prove the effect of throwing exceptions on performance in. Net core
【滤波器】基于matlab时变维纳滤波器设计【含Matlab源码 1870期】
C language - data storage
@How to resolve controller and requestmapping
[interface] view the interface path and check the interface
Netstat command details