C/C++语言参数传递----值传递、引用传递、指针传递、指针引用传递

来源:互联网 发布:如何变漂亮 知乎 编辑:程序博客网 时间:2024/04/29 02:47

1.值传递

[cpp] view plain copy
  1. void f( int  p){  
  2.     printf("\n%x",&p);  
  3.     printf("\n%x",p);  
  4.     p=0xff;  
  5. }  
  6. void main()  
  7. {  
  8.     int a=0x10;  
  9.     printf("\n%x",&a);  
  10.     printf("\n%x\n",a);  
  11.     f(a);  
  12.     printf("\n%x\n",a);  
  13. }  


通过上例我们可以看到,int a=0x10,存放的地址为0x12ff44,值为10,当调用f(a)时,传递给p的值为10,但是p的地址为0x12fef4,当改变p=0xff,时是改变地址为0x12fef4中的内容,并没有改变0x12ff44中的内容,所以调用f(a),后a的值仍然为0x10,所以值传递无法改变变量的值。示意图如下:


2.引用传递

[cpp] view plain copy
  1. void f( int & p){  
  2.     printf("\n%x",&p);  
  3.     printf("\n%x",p);  
  4.     p=0xff;  
  5. }  
  6. void main()  
  7. {  
  8.     int a=0x10;  
  9.     printf("\n%x",&a);  
  10.     printf("\n%x\n",a);  
  11.     f(a);  
  12.     printf("\n%x\n",a);  
  13. }  

通过上面引用传递传递案例我们可以看到,调用f(a)时,传递给p的是a的地址,所以p和a的地址都是0X12ff44,所以p就是a,改变p当然能改变a。示意图如下:

3.指针传递

[cpp] view plain copy
  1. void f( int*p){  
  2.     printf("\n%x",&p);  
  3.     printf("\n%x",p);  
  4.     printf("\n%x\n",*p);  
  5.     *p=0xff;  
  6. }  
  7. void main()  
  8. {  
  9.     int a=0x10;  
  10.     printf("\n%x",&a);  
  11.     printf("\n%x\n",a);  
  12.     f(&a);  
  13.     printf("\n%x\n",a);  
  14. }  



通过指针传递的案例我们可以看到,调用f(&a)是将a的地址0x12ff44传递给p,则*p就指向了a的内容,改变*p后,a的内容自然就改变了,示意图如下:


4.指针的引用传递

[cpp] view plain copy
  1. void f( int*&p){  
  2.     printf("\n%x",&p);  
  3.     printf("\n%x",p);  
  4.     printf("\n%x\n",*p);  
  5.     *p=0xff;  
  6. }  
  7. void main()  
  8. {  
  9.     int a=0x10;  
  10.     printf("\n%x",&a);  
  11.     printf("\n%x\n",a);  
  12.     int *b=&a;  
  13.     printf("\n%x",&b);  
  14.     printf("\n%x",b);  
  15.     printf("\n%x\n",*b);  
  16.     f(b);  
  17.     printf("\n%x\n",a);  
  18. }  



为了使用指针的引用传递我们要新建一个指针b,然后将b的引用传递给p,其实p就是b的一个拷贝,*p=*b都指向a,所以改变*p的内容也就改变a的内容。示意图如下:

我们再来看一下如果不用指针的引用传递会出现什么结果
[cpp] view plain copy
  1. void f( int*p){  
  2.     printf("\n%x",&p);  
  3.     printf("\n%x",p);  
  4.     printf("\n%x\n",*p);  
  5.     *p=0xff;  
  6. }  
  7. void main()  
  8. {  
  9.     int a=0x10;  
  10.     printf("\n%x",&a);  
  11.     printf("\n%x\n",a);  
  12.     int *b=&a;  
  13.     printf("\n%x",&b);  
  14.     printf("\n%x",b);  
  15.     printf("\n%x\n",*b);  
  16.     f(b);  
  17.     printf("\n%x\n",a);  
  18.     printf("\n%x\n",b);  
  19. }  


从结果中我们可以看到调用f(b)时,传递给p的是b的内容,但是&b,和&p是不一样的,虽然*p和*b都指向a。示意图如下:


5.错误案例


[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <string.h>  
  4.   
  5. void Allocate(char* p,int size){  
  6.   
  7.     printf("\n%x",&p);  
  8.     printf("\n%x",p);  
  9.   
  10.     p=(char*)malloc(size);  
  11. }  
  12. void Free(char* p){  
  13.     free(p);  
  14. }  
  15. void main()  
  16. {  
  17.     char *str=NULL;  
  18.     printf("\n%X",&str);  
  19.     printf("\n%X",str);  
  20.     Allocate(str,100);  
  21.     strcpy(str,"Hello World!");  
  22.     printf("\n%s",str);  
  23.     Free(str);  
  24.     printf("\nstr=%s",str);  
  25.       
  26. }  



当执行strcpy(str,"Hello World!"),时会报Unhandled exception in CPoint.exe:0xC0000005:Access Violation,这是因为我们参用的是指针传递,从运行结果我们可以看到str的地址为0x12ff44,当调用Allocate(str,100)时,传递给p的是str,的内容也就是0,所以p为0,但是&p并不是和&str一样的,所以在运行p=(char*)malloc(size)时,是给0x12fef0分配的100个字节,并没有给0x12ff44分配字节,所以*str还是空。所以会报错。

5.正确案例

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <string.h>  
  4.   
  5. void Allocate(char*& p,int size){  
  6.     printf("\n%x",&p);  
  7.     printf("\n%x",p);  
  8.     p=(char*)malloc(size);  
  9. }  
  10. void Free(char* p){  
  11.     free(p);  
  12. }  
  13. void main()  
  14. {  
  15.     char *str=NULL;  
  16.     printf("\n%X",&str);  
  17.     printf("\n%X",str);  
  18.     Allocate(str,100);  
  19.     strcpy(str,"Hello World!");  
  20.     printf("\n%s",str);  
  21.     Free(str);    
  22. }  



因为指针引用传递的是指针的拷贝,所以&str和&p,是地址是一样的,所以对p分配内容空间也就是对str分配空间,所以没有问题!
0 0
原创粉丝点击