C语言函数参数传递的分析

简介:
+关注继续查看

一、值传递和指针传递的概念

(一)值传递

值传递,即按值传递参数,按值传递参数时,是将实参变量的值复制一个到临时存储单元中,如果在调用过程中改变了形参的值,不会影响实参变量本身,即实参变量保持调用前的值不变。

1、形参只能是变量,实参可以是常量、变量或表达式。在被定义的函数中,必须指定形参的类型。

2、实参与形参的个数应一样,类型应一致。字符型和整型可以互相通用。

4、实参传递给形参是实参的值,形参变量在未出现函数调用时,并不占用内存,只在调用时才占用。调用结束后,将释放内存。值传递过程中参数的数据传递是单向的,数据(实参的值)从实参传递形参,而不能由形参传回实参。执行一个被调用函数时,形参的值如果发生改变,并不会改变主调函数中的实参的值。

4、形参如同公式中的符号,实参就是符号具体的值,在调用过程前必须得到赋值;调用过程就是实现形参与实参的结合,把实参的值通过调用传递给形参,相当于把值代入公式进行计算。值传递的本质就是表达式。

(二)地址传递

地址传递,即按地址传递参数,按地址传递参数时,把实参变量的地址传送给被调用过程,形参和实参共用内存的同一地址。在被调用过程中,形参的值一旦改变,相应实参的值也跟着改变。

1、实参必须是是变量,也就是保证可以重新被赋值或者初始化。在被定义的函数中,必须指定形参的类型。

2、实参与形参的个数应一样,类型应一致。字符型和整型可以互相通用。

3、实参传递给形参的是实参变量的地址,函数调用过程中,并不为形参开辟存储空间,也就是说地址传递过程中形参和实参共用实参的存储空间,对形参的操作就是对实参本身的操作。

(三)值传递,地址传递的区别

1、值传递实参向形参传递的是实参的值,而地址传递传递的却是实参的地址。

2、值传递在函数调用过程中会为形参重新开辟空间,形参与实参分别占用不同的地址空间,而地址传递,形参和实参共用同一内存地址。

我们在参数传递过程中,只要抓住这两点区别,就很好区别参数传递的具体方式。

二、C语言函数参数的传递规律

下面我们将从形参和实参是普通类型变量、指针变量、数组名时分别讨论参数的传递方式。

(一)普通类型变量的参数传递

简单类型变量作实参,形参对应为类型一致的简单类型变量,请看下面的程序

  1. void fun_of_value(int par_value)  
  2. {  
  3. printf("In function, the Address of the VALUE = %p\n", &par_value);  
  4. printf("In function, the Value of the VALUE = %d\n\n\n", par_value);  
  5. }  
  6. int main(void)  
  7. {  
  8. int arg_value = 10;  
  9. printf("In main, the Address of the VALUE = %p\n", &arg_value);  
  10. printf("In main, the Value of the VALUE = %d\n", arg_value);  
  11. fun_of_value(value);  
  12. printf("After function, the Address of the VALUE = %p\n", &arg_value);  
  13. printf("After function, the Value of the VALUE = %d\n", arg_value);  
  14. }  
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中的运行结果

  1. In main, the Address of the VALUE = 0028feec  
  2. In main, the Value of the VALUE = 10  
  3. In function, the Address of the VALUE = 0028fed0  
  4. 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、普通指针的作为参数

  1. void fun_of_point(int *par_point)  
  2. {  
  3. printf("In function, the Address of the POINT = %p\n", &par_point);  
  4. printf("In function, the Value of the POINT = %p\n\n\n", par_point);  
  5. }  
  6. int main(void)  
  7. {  
  8. int  value = 10;  
  9. int *arg_point = &value;  
  10. printf("The Addreess of the value = %p\n", &value);  
  11. printf("In main, the Address of the POINT = %p\n", &arg_point);  
  12. printf("In main, the Value of the POINT = %p\n", arg_point);     // 打印参数的值  
  13. fun_of_point(point);  
  14. return EXIT_SUCCESS;  
  15. }  
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中的运行结果

  1. The Addreess og the value 0028feec  
  2. In main, the Address of the POINT = 0028fee8  
  3. In main, the Value of the POINT = 0028feec  
  4. In function, the Address of the POINT = 0028fed0  
  5. 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、函数指针的参数

  1. void function(void){}  
  2. void fun_of_fun_point(void(*par_pfun)(void))  
  3. {  
  4. printf("In function, the Address of the FUNCTION = %p\n", &par_pfun);  
  5. printf("In function, the Value of the FUNCTION = %p\n\n\n", par_pfun);  
  6. }  
  7. int main(void)  
  8. {  
  9. void (*arg_pfun)(void);  
  10. arg_pfun = /*&*/function;  
  11. printf("The Address of the FUNCTION = %p\n", function);  
  12. printf("In main, the Address of the FUNCTION = %p\n", &arg_pfun);  
  13. printf("In main, the Value of the FUNCTION = %p\n", arg_pfun);  
  14. fun_of_fun_point(arg_pfun);  
  15. return EXIT_SUCCESS;  
  16. }  
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;
}


运行结果如下:

  1. The Address of the FUNCTION = 004016c9  
  2. In main, the Address of the FUNCTION = 0028feec  
  3. In main, the Value of the FUNCTION = 004016c9  
  4. In function, the Address of the FUNCTION = 0028fed0  
  5. 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的地址。

(三)数组名的参数传递

  1. void fun_of_array(int array[])  
  2. {  
  3. printf("In function, the Address of the ARRAY = %p\n", &array);  
  4. printf("In function, the Value of the ARRAY = %p\n", array);  
  5. }  
  6. void fun_of_array_point(int *array)  
  7. {  
  8. printf("In function, the Address of the ARRAYPOINT = %p\n", &array);  
  9. printf("In function, the Value of the ARRAYPOINT = %p\n", array);  
  10. }  
  11. int main(void)  
  12. {  
  13. int array[10];  
  14. printf("The Addres of the first value %p\n", &array[0]);  
  15. printf("In main, the Address of the ARRAY = %p\n", &array);  
  16. printf("In main, the Value of the ARRAY = %p\n", array);  
  17. fun_of_array(array);  
  18. fun_of_array_point(array);  
  19. return EXIT_SUCCESS;  
  20. }  
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;
}


程序的运行结果如下:

  1. The Addres of the first value 0028fec8  
  2. In main, the Address of the ARRAY = 0028fec8  
  3. In main, the Value of the ARRAY = 0028fec8  
  4. In function, the Address of the ARRAY = 0028feb0  
  5. In function, the Value of the ARRAY = 0028fec8  
  6. In function, the Address of the ARRAYPOINT = 0028feb0  
  7. 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

目录
相关文章
|
1月前
|
算法 C语言
【零钱问题】C语言贪心算法分析(文末彩蛋)
【零钱问题】C语言贪心算法分析(文末彩蛋)
23 0
|
2月前
|
算法
(C语言版)力扣(LeetCode)27.移除元素三种解法分析
给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
|
2月前
|
算法
(C语言版)力扣(LeetCode)189. 轮转数组官方3种解法分析
尽可能想出更多的解决方案,至少有 三种 不同的方法可以解决这个问题。
|
3月前
|
存储 编译器 C语言
确定不进来看看吗?详细讲解C语言文件操作(示例分析每个函数)(下)
确定不进来看看吗?详细讲解C语言文件操作(示例分析每个函数)
37 1
|
3月前
|
存储 C语言
确定不进来看看吗?详细讲解C语言文件操作(示例分析每个函数)(中)
确定不进来看看吗?详细讲解C语言文件操作(示例分析每个函数)
27 0
|
3月前
|
程序员 编译器 C语言
确定不进来看看吗?详细讲解C语言文件操作(示例分析每个函数)(上)
确定不进来看看吗?详细讲解C语言文件操作(示例分析每个函数)
51 0
|
3月前
|
C语言
一直没有搞懂的C语言参数传递,今天终于明白了
一直没有搞懂的C语言参数传递,今天终于明白了
33 0
|
5月前
|
算法 测试技术 C语言
【C语言蓝桥杯每日一题】—— 单词分析
题目描述 小蓝正在学习一门神奇的语言,这门语言中的单词都是由小写英文字母组 成,有些单词很长,远远超过正常英文单词的长度。小蓝学了很长时间也记不住一些单词,他准备不再完全记忆这些单词,而是根据单词中哪个字母出现得最多来分辨单词。 现在,请你帮助小蓝,给了一个单词后,帮助他找到出现最多的字母和这 个字母出现的次数。 输入描述 输入一行包含一个单词,单词只由小写英文字母组成。 对于所有的评测用例,输入的单词长度不超过 1000。 输出描述 输出两行,第一行包含一个英文字母,表示单词中出现得最多的字母是哪 个。如果有多个字母出现的次数相等,输出字典序最小的那个。
130 0
【C语言蓝桥杯每日一题】—— 单词分析
|
5月前
|
C语言
C语言中typedef和define对比分析
C语言中typedef和define对比分析
54 0
|
7月前
|
存储 Go C语言
编译原理,C语言实现LR(0)分析(扩展文法的生成、项目集规范簇的生成、ACTION GOTO表的生成、句子的分析)
注:代码生成的项目集规范簇、ACTION GOTO表的顺序可能和课本、教材、参考答案的顺序不同,但这并不影响分析过程的正确性,毕竟机器是按规律办事的😄
134 0
编译原理,C语言实现LR(0)分析(扩展文法的生成、项目集规范簇的生成、ACTION GOTO表的生成、句子的分析)
相关产品
云迁移中心
推荐文章
更多