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

╰半橙微兮° 2022-09-17 12:26 380阅读 0赞

1.值传递

  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. }

20131020215707531

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

Center

2.引用传递

  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. }

20131020215623390

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

20131021104454656

3.指针传递

  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. }

Center 1

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

20131021110737953

4.指针的引用传递

  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. }

20131020215430093

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

" class="reference-link">20131021111429937

我们再来看一下如果不用指针的引用传递会出现什么结果

  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. }

20131021112043156

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

20131021112458390

5.错误案例

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

20131021113652125

当执行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.正确案例

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

20131021114713906

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

发表评论

表情:
评论列表 (有 0 条评论,380人围观)

还没有评论,来说两句吧...

相关阅读

    相关 传递引用传递指针传递

    一、值传递 1、特点: (1)形参是实参的拷贝,改变形参的值并不会影响外部实参的值。 (2)从被调用函数的角度来说,值传递是单向的(实参->形参),参数的值只能传入,不能