一、值传递和指针传递的概念
(一)值传递
值传递,即按值传递参数,按值传递参数时,是将实参变量的值复制一个到临时存储单元中,如果在调用过程中改变了形参的值,不会影响实参变量本身,即实参变量保持调用前的值不变。
1、形参只能是变量,实参可以是常量、变量或表达式。在被定义的函数中,必须指定形参的类型。
2、实参与形参的个数应一样,类型应一致。字符型和整型可以互相通用。
4、实参传递给形参是实参的值,形参变量在未出现函数调用时,并不占用内存,只在调用时才占用。调用结束后,将释放内存。值传递过程中参数的数据传递是单向的,数据(实参的值)从实参传递形参,而不能由形参传回实参。执行一个被调用函数时,形参的值如果发生改变,并不会改变主调函数中的实参的值。
4、形参如同公式中的符号,实参就是符号具体的值,在调用过程前必须得到赋值;调用过程就是实现形参与实参的结合,把实参的值通过调用传递给形参,相当于把值代入公式进行计算。值传递的本质就是表达式。
(二)地址传递
地址传递,即按地址传递参数,按地址传递参数时,把实参变量的地址传送给被调用过程,形参和实参共用内存的同一地址。在被调用过程中,形参的值一旦改变,相应实参的值也跟着改变。
1、实参必须是是变量,也就是保证可以重新被赋值或者初始化。在被定义的函数中,必须指定形参的类型。
2、实参与形参的个数应一样,类型应一致。字符型和整型可以互相通用。
3、实参传递给形参的是实参变量的地址,函数调用过程中,并不为形参开辟存储空间,也就是说地址传递过程中形参和实参共用实参的存储空间,对形参的操作就是对实参本身的操作。
(三)值传递,地址传递的区别
1、值传递实参向形参传递的是实参的值,而地址传递传递的却是实参的地址。
2、值传递在函数调用过程中会为形参重新开辟空间,形参与实参分别占用不同的地址空间,而地址传递,形参和实参共用同一内存地址。
我们在参数传递过程中,只要抓住这两点区别,就很好区别参数传递的具体方式。
二、C语言函数参数的传递规律
下面我们将从形参和实参是普通类型变量、指针变量、数组名时分别讨论参数的传递方式。
(一)普通类型变量的参数传递
简单类型变量作实参,形参对应为类型一致的简单类型变量,请看下面的程序
- void fun_of_value(int par_value)
- {
- printf("In function, the Address of the VALUE = %p\n", &par_value);
- printf("In function, the Value of the VALUE = %d\n\n\n", par_value);
- }
- int main(void)
- {
- int arg_value = 10;
- printf("In main, the Address of the VALUE = %p\n", &arg_value);
- printf("In main, the Value of the VALUE = %d\n", arg_value);
- fun_of_value(value);
- printf("After function, the Address of the VALUE = %p\n", &arg_value);
- printf("After function, the Value of the VALUE = %d\n", arg_value);
- }
void fun_of_value(int par_value) { printf("In function, the Address of the VALUE = %p\n", &par_value); printf("In function, the Value of the VALUE = %d\n\n\n", par_value); } int main(void) { int arg_value = 10; printf("In main, the Address of the VALUE = %p\n", &arg_value); printf("In main, the Value of the VALUE = %d\n", arg_value); fun_of_value(value); printf("After function, the Address of the VALUE = %p\n", &arg_value); printf("After function, the Value of the VALUE = %d\n", arg_value); }
我们在主程序和函数中分别打印实参arg_value和形参par_value的地址和值,下面是程序在Code::Block12.11中的运行结果
- In main, the Address of the VALUE = 0028feec
- In main, the Value of the VALUE = 10
- In function, the Address of the VALUE = 0028fed0
- In function, the Value of the VALUE = 10
In main, the Address of the VALUE = 0028feec In main, the Value of the VALUE = 10 In function, the Address of the VALUE = 0028fed0 In function, the Value of the VALUE = 10
简易的内存图示如下:
很显然在参数传递的过程中,实参arg_value和形参par_value具有相同的值,但是占用的存储单元不相同,在函数中对形参的运算,对实参没有影响,说明简单类型变量在参数传递过程中是单向值传递的。
(二)指针变量的参数传递
1、普通指针的作为参数
- void fun_of_point(int *par_point)
- {
- printf("In function, the Address of the POINT = %p\n", &par_point);
- printf("In function, the Value of the POINT = %p\n\n\n", par_point);
- }
- int main(void)
- {
- int value = 10;
- int *arg_point = &value;
- printf("The Addreess of the value = %p\n", &value);
- printf("In main, the Address of the POINT = %p\n", &arg_point);
- printf("In main, the Value of the POINT = %p\n", arg_point); // 打印参数的值
- fun_of_point(point);
- return EXIT_SUCCESS;
- }
void fun_of_point(int *par_point) { printf("In function, the Address of the POINT = %p\n", &par_point); printf("In function, the Value of the POINT = %p\n\n\n", par_point); } int main(void) { int value = 10; int *arg_point = &value; printf("The Addreess of the value = %p\n", &value); printf("In main, the Address of the POINT = %p\n", &arg_point); printf("In main, the Value of the POINT = %p\n", arg_point); // 打印参数的值 fun_of_point(point); return EXIT_SUCCESS; }同样我们在主程序和函数中分别打印实参arg_value和形参par_value的地址和值,下面是程序在Code::Block12.11中的运行结果
- The Addreess og the value 0028feec
- In main, the Address of the POINT = 0028fee8
- In main, the Value of the POINT = 0028feec
- In function, the Address of the POINT = 0028fed0
- In function, the Value of the POINT = 0028feec
The Addreess og the value 0028feec In main, the Address of the POINT = 0028fee8 In main, the Value of the POINT = 0028feec In function, the Address of the POINT = 0028fed0 In function, the Value of the POINT = 0028feec
对结果分析后的图示如下:
指针存储的值其实是变量value的地址,我们发现,当参数是普通指针类型的时候,形参和实参都存储着变量value的地址,也就是值相同,但是他们本身所占用的存储单元却不相同,即不共用存储空间,我们不难得出当参数类型是普通指针变量时,参数传递也是值传递,只是传递的值是地址(实参的值),并不是“地址传递”。
2、函数指针的参数
- void function(void){}
- void fun_of_fun_point(void(*par_pfun)(void))
- {
- printf("In function, the Address of the FUNCTION = %p\n", &par_pfun);
- printf("In function, the Value of the FUNCTION = %p\n\n\n", par_pfun);
- }
- int main(void)
- {
- void (*arg_pfun)(void);
- arg_pfun = /*&*/function;
- printf("The Address of the FUNCTION = %p\n", function);
- printf("In main, the Address of the FUNCTION = %p\n", &arg_pfun);
- printf("In main, the Value of the FUNCTION = %p\n", arg_pfun);
- fun_of_fun_point(arg_pfun);
- return EXIT_SUCCESS;
- }
void function(void){} void fun_of_fun_point(void(*par_pfun)(void)) { printf("In function, the Address of the FUNCTION = %p\n", &par_pfun); printf("In function, the Value of the FUNCTION = %p\n\n\n", par_pfun); } int main(void) { void (*arg_pfun)(void); arg_pfun = /*&*/function; printf("The Address of the FUNCTION = %p\n", function); printf("In main, the Address of the FUNCTION = %p\n", &arg_pfun); printf("In main, the Value of the FUNCTION = %p\n", arg_pfun); fun_of_fun_point(arg_pfun); return EXIT_SUCCESS; }
运行结果如下:
- The Address of the FUNCTION = 004016c9
- In main, the Address of the FUNCTION = 0028feec
- In main, the Value of the FUNCTION = 004016c9
- In function, the Address of the FUNCTION = 0028fed0
- In function, the Value of the FUNCTION = 004016c9
The Address of the FUNCTION = 004016c9 In main, the Address of the FUNCTION = 0028feec In main, the Value of the FUNCTION = 004016c9 In function, the Address of the FUNCTION = 0028fed0 In function, the Value of the FUNCTION = 004016c9对结果分析后的图示如下:
与前面采取的方式类似,以函数指针作为参数与普通指着一样,实参与形参都存储了原函数的地址,但是他们本身的所占用的存储单元并不相同,可见此时参数传递方式仍然是值传递,传递的值是函数function的地址。
(三)数组名的参数传递
- void fun_of_array(int array[])
- {
- printf("In function, the Address of the ARRAY = %p\n", &array);
- printf("In function, the Value of the ARRAY = %p\n", array);
- }
- void fun_of_array_point(int *array)
- {
- printf("In function, the Address of the ARRAYPOINT = %p\n", &array);
- printf("In function, the Value of the ARRAYPOINT = %p\n", array);
- }
- int main(void)
- {
- int array[10];
- printf("The Addres of the first value %p\n", &array[0]);
- printf("In main, the Address of the ARRAY = %p\n", &array);
- printf("In main, the Value of the ARRAY = %p\n", array);
- fun_of_array(array);
- fun_of_array_point(array);
- return EXIT_SUCCESS;
- }
void fun_of_array(int array[]) { printf("In function, the Address of the ARRAY = %p\n", &array); printf("In function, the Value of the ARRAY = %p\n", array); } void fun_of_array_point(int *array) { printf("In function, the Address of the ARRAYPOINT = %p\n", &array); printf("In function, the Value of the ARRAYPOINT = %p\n", array); } int main(void) { int array[10]; printf("The Addres of the first value %p\n", &array[0]); printf("In main, the Address of the ARRAY = %p\n", &array); printf("In main, the Value of the ARRAY = %p\n", array); fun_of_array(array); fun_of_array_point(array); return EXIT_SUCCESS; }
程序的运行结果如下:
- The Addres of the first value 0028fec8
- In main, the Address of the ARRAY = 0028fec8
- In main, the Value of the ARRAY = 0028fec8
- In function, the Address of the ARRAY = 0028feb0
- In function, the Value of the ARRAY = 0028fec8
- In function, the Address of the ARRAYPOINT = 0028feb0
- In function, the Value of the ARRAYPOINT = 0028fec8
The Addres of the first value 0028fec8 In main, the Address of the ARRAY = 0028fec8 In main, the Value of the ARRAY = 0028fec8 In function, the Address of the ARRAY = 0028feb0 In function, the Value of the ARRAY = 0028fec8 In function, the Address of the ARRAYPOINT = 0028feb0 In function, the Value of the ARRAYPOINT = 0028fec8我们不难看出,数组的地址和“数组名的值”(我们暂且这样称呼)都存储了数组的首地址,其实大多数编译器(如gcc)在处理的时候会在预处理阶段把数组名类型的参数转换为指针类型的参数进行处理的,在编译器看来,在以数组名作为参数的时候数组参数本质上为指针参数,即数组参数和指针参数是完全等价的两种形式,
因此以数组名作为参数,本质上也是指针类型的参数传递,也采用单向值传递的方式。
三 总结
由此我们不难得出结论,C语言函数的所有参数传递均以“按值传递”(单向值传递)方式进行,在函数内部将获得与形式参数值相同的一份数据拷贝,函数的参数传递采用单向的值传递方式。当指针作为函数参数时,传递值是指针的值,即地址,依然是单向的值传递方式,并不是双向的数据传递方式。
注意:引用传递是C99新引入的参数传递机制,但是目前为止,仍没有一款编译器完全支持C99标准,因此引用传递在此不进行讨论,但是通过对资料的研究,其实引用传递本质也是采用指针传递的机制来实现的。
转载:http://blog.csdn.net/gatieme/article/details/17657801