当前位置:网站首页>Pass value, reference and pointer
Pass value, reference and pointer
2022-07-18 07:06:00 【Smile Hun】
Pass value
C in , all Non array form The data arguments of are Value passing form , Value passing is when calling a function , Copy a copy of the arguments to the calling function , That is to say, the function operates Copy of arguments and Not the argument itself . In a subfunction , For passed parameters ( Formal parameters ) Changes made Can't Act on the original variable .
#include <stdio.h>
void test(int x, int y);
int main() {
int a = 1, b = 2;
test(a, b);// What is passed here is a,b Value , Next, in the subfunction test() in , Yes x,y The operation of will not affect a,b Original value of
printf("%d %d", a, b);
}
void test(int x, int y) {
int tem;
tem = x;
x = y;
y = tem;
}
Next is the monitoring of variables , You can see , After executing the code ,x,y The values of are interchanged and become 2,1, however Original value a,b Still 1,2
The reference
quote and The original variables It's a thing , Just change a name
The reference In essence, it passes the original variable Address
After having the address , The operation of variables will directly modify the values existing in the place , That is, the operation of variables in sub functions will directly act on the original variables .
#include <stdio.h>
void test(int &x, int &y);
/* The formal parameter of the subfunction has an address character , When a sub function is called , It will directly take the address of the passed parameters , Make later right x,y The operation of acts directly on the original variable */
int main() {
int a = 1, b = 2;
test(a, b);
printf("%d %d", a, b);
}
void test(int &x, int &y) {
int tem;
tem = *x;
*x = *y;
*y = tem;
}
The pointer
The essence of pointer passing is Pass value , But this value is the original variable Address .
#include <stdio.h>
void test(int *x, int *y);
int main() {
int a = 1, b = 2;
test(&a, &b);
printf("%d %d", a, b);
}
void test(int *x, int *y) {
int tem;
tem = *x;
*x = *y;
*y = tem;
}
Monitor page 
You can see x,y The value of is a binary number , On behalf of a and b Of Address , The curly braces in the back refer to the address value
边栏推荐
- Application of UNET in battery segmentation project
- PD QC诱骗取电应用IC《乐得瑞LDR6328S》广泛应用于各大小家电
- php 生成excel表的2种方法
- Mysql高版本报sql_mode=only_full_group_by异常
- 浅析PHP关键词替换的类(避免重复替换,保留与还原原始链接)
- Resnet50 structure diagram
- Flex & bison advanced calculator
- 9. MySQL -- JDBC入门
- Notes on custom types such as structs, enumerations, unions, etc
- Is there a limit on the length of string? How much is it?
猜你喜欢
随机推荐
RuntimeWarning: overflow encountered in long_ scalars h = 12.0 / (totaln * (totaln + 1)) * ssbn - 3
FFmpeg sample 分析:muxing.c
C language Chapter 8 array
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (4)
Debug system
WM8960声卡相关问题
TP5查询空的2种情况
60 year old developer's suggestion, try to change it!
Implementation of sequence table in C language stack
Chapter 7 preprocessing of C language
Enumeration, do you know it?
[problems of dft/fft - solutions to fence effect]
sensor 原理文章
Yotact structure diagram
Type-c充电OTG芯片(LDR6028S)
COMS技术
Cron表达式使用
What is base?
Ffmpeg sample analysis: muting c
H264-解码顺序 显示顺序 参考顺序









