1、关于函数参数的传递方式:
值传递:只是将参数值copy函数中,因此在函数中无法修改该参数的值。修改的只是传入参数的一份copy.
引用传递:将参数地址传给函数,因此在函数中能修改该参数的值
- #define Fun_No_Error 1
- #include <stdio.h>
- /*
- 该程序用来验证,函数中参数的值传递,与引用传递的不同点
- */
- //Value deliver
- void FucValue(int a,int b)
- {
- printf("/nAddress of a is %d",&a);
- printf("/nAddress of b is %d",&b);
- a=a+1;
- b=b+1;
- }
- //refer deliver
- void FucRefer(int &a,int &b)
- {
- printf("/nAddress of a is %d",&a);
- printf("/nAddress of b is %d",&b);
- a=a+1;
- b=b+1;
- }
- int main(int argc,int argv[])
- {
- int input1,input2,input3,input4;
- printf("Please input two integer:");
- scanf("%d",&input1);
- scanf("%d",&input2);
- printf("/nAddress of input1 is %d",&input1);
- printf("/nAddress of input2 is %d",&input2);
- FucValue(input1,input2);
- printf("/ninput1=%d/ninput2=%d",input1,input2);
- printf("/nPlease input two integer:");
- scanf("%d",&input3);
- scanf("%d",&input4);
- printf("/nAddress of input1 is %d",&input3);
- printf("/nAddress of input2 is %d",&input4);
- FucRefer(input3,input4);
- printf("/ninput1=%d/ninput2=%d",input3,input4);
- }
本文通过一个C语言程序实例,演示了函数参数值传递与引用传递的区别。在值传递中,函数内部对参数的修改不会影响到实参;而在引用传递中,函数内部对参数的修改会影响到实参的值。

被折叠的 条评论
为什么被折叠?



