当前位置:网站首页>C语言的传参方式(int x)(int *x)(int &x)
C语言的传参方式(int x)(int *x)(int &x)
2022-07-29 09:25:00 【爱打羽毛球的程序员】
本人坚持更新C语言,数据结构,操作系统,前端知识,可以收藏+关注随时了解
目录
C语言一共有三种传参方式
1.传数值(int x)
void test(int x){
}
就是把你的变量的值传递
给函数的形式参数,实际就是用变量的值来新生成一个形式参数,因而在函数里对形参的改变不会影响到函数外的变量的值。
注意:形参和实参永远不是一个变量
最经典的例子就是交换两个数的值, 当函数调用的时候,实参传给形参时,形参将是实参的一份临时拷贝,所以对形参的修改是不影响实参的,如果用这种方式传参,只是交换了形参的值,不会真正的交换两个数的值
void exchange(int a, int b)
{
int t;//定义中间变量
t = a;
a = b;
b = temp;
}
int main(int argc, char const *argv[])
{
int a = 3;
int b = 5;
exchange(a, b);
printf("----------------------\n");
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}
2.传地址(int *x)
就是传变量的地址赋给函数里形式参数的指针,使指针指向真实的变量的地址,因为对指针所指地址的内容的改变能反映到函数外,也就是能改变函数外的变量的值。
void exchange_3(int *p, int *q)
{
int t;
//如果要互换*p和*q的值,则t必须定义为int,不能定义成int*,否则语法错误
t = *p; //p的数据类型是int*,*p的数据类型是int
*p = *q;
*q = t;
}
int main(int argc, char const *argv[])
{
int a = 3;
int b = 5;
exchange_3(&a, &b);
printf("----------------------\n");
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}
但是值得注意的是,指针变量的大小永远只占4个字节,所以用指针来传递的话,会更快,效率也更高。
3.传引用(int &x)
引用实际上是某一个变量的别名,和这个变量具有相同的内存空间。
实参把变量传递给形参引用,相当于形参是实参变量的别名,对形参的修改都是直接修改实参。
在类的成员函数中经常用到类的引用对象作为形参,大大的提高代码的效率
例如我们在初始化一个栈的时候
int IninStack(SqStack &S)
{
S.base = (int *)malloc(STACKSIZEINIT * sizeof(int));
if (!S.base)
{
exit(-1); //内存分配失败,退出程序
}
S.top = S.base; //栈顶指针和栈底指针指在同一块存储单元
S.stacksize = STACKSIZEINIT;
return 1;
}
总结
声明一个引用,不是新定义了一个变量,它只表示该引用名是目标变量名的一个别名,它本身不是一种数据类型,因此引用本身不占存储单元,系统也不给引用分配存储单元。故:对引用求地址,就是对目标变量求地址。&ra与&a相等。
引用传递可以看成在值传递的基础上,在函数定义和声明的形参变量前加一个 &,其它的使用和值传递完全相同,因此也看出引用传递更加方便
边栏推荐
- Network knowledge summary
- Leetcode: interview question 08.14. Boolean operation
- Outlook tutorial, how to create an electronic signature in outlook?
- (Video + graphics) introduction to machine learning series - Chapter 1 Introduction
- mysql怎么换成中文
- Vs2015 uses loadlibrary to call DLL library
- Rocky基础之编译安装apache
- 常用的DOS命令[逐渐完善]
- 查看端口占用情况
- Data representation and calculation (base)
猜你喜欢
网络安全(5)
Qmainwindow details
MySQL error summary
No swagger, what do I use?
Handwritten character recognition
Acwing game 59 [End]
不用Swagger,那我用啥?
Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System
smart-webcomponents 14.2.0 Crack
随机推荐
23 postgraduate entrance examination people hold on! The first wave of exam abandonment peak has arrived!
The use and Simulation of string function, character function and memory function
使用cpolar发布树莓派网页(cpolar隧道的完善)
201803-3 CCF URL映射 满分题解
Parameter initialization
【Unity入门计划】C#与Unity-了解类和对象
36. JS animation
先序遍历/后序遍历确定树的大致形态
Leetcode:132. split palindrome string II
No swagger, what do I use?
One article tells you the salary after passing the PMP Exam
如何介绍自己的项目经验?
【集中培训】HCIP-Cloud Computing 资源交流帖
【BERT-多标签文本分类实战】之一——实战项目总览
Data type of MySQL
[unity entry program] C # and unity - understand classes and objects
远程连接windows版本服务器redis的配置文件设置
What should I pay attention to now? Excuse me, is it safe to open a stock account by mobile phone?
Outlook tutorial, how to create an electronic signature in outlook?
怎么样的框架对于开发者是友好的?