当前位置:网站首页>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相等。
引用传递可以看成在值传递的基础上,在函数定义和声明的形参变量前加一个 &,其它的使用和值传递完全相同,因此也看出引用传递更加方便
边栏推荐
- Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
- Explanation of trie tree (dictionary tree)
- 如何为OpenHarmony做贡献
- How does xjson implement four operations?
- Vs2015 uses loadlibrary to call DLL library
- 文件上传及拓展
- [centralized training] hcip cloud computing resource exchange post
- 先序遍历/后序遍历确定树的大致形态
- 【机器学习】逻辑回归代码练习
- (视频+图文)机器学习入门系列-第1章 引言
猜你喜欢

Leetcode: interview question 08.14. Boolean operation

Emmet syntax
数据表示与计算(进制)

Basic part 2 of flowable

Introduction to translation professional qualification (level) examination

AxureRP原型设计 快速开始

使用cpolar发布树莓派网页(cpolar隧道的完善)

浅谈契约测试

Database system design: partition

Four types of technical solutions shared by distributed sessions, and their advantages and disadvantages
随机推荐
Emmet syntax
Basic part 2 of flowable
mysql将部分表名统一转换为大写
1.2.24 fastjson deserialization templatesimpl uses chain analysis (very detailed)
【集中培训】HCIP-Cloud Computing 资源交流帖
使用cpolar发布树莓派网页(cpolar功能的完善)
Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
dataframe. to_ Sql() inserts too many errors at one time
Floweable foundation Chapter 1
网络安全(5)
C# 使用RestSharp库实现POST请求
VS2015采用loadlibrary方式调用dll库
【机器学习】朴素贝叶斯代码练习
【云驻共创】【HCSD大咖直播】亲授大厂面试秘诀
Introduction to translation professional qualification (level) examination
A structured random inactivation UNET for retinal vascular segmentation
Database system design: partition
[machine learning] logistic regression code exercise
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
dataframe.to_sql() 一次性插入过多报错