鱼C论坛

 找回密码
 立即注册
查看: 988|回复: 1

[已解决]指针变量,形参,实参

[复制链接]
发表于 2020-12-12 16:22:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. void swap(int *px, int *py)
  2. {
  3.    int tmp=*px;
  4.    *px=*py;
  5.    *py=tmp;
  6.     printf("*px=%d,*py=%d\n",*px,*py);
  7. }
复制代码
  1. void swap(int *px, int *py)
  2. {
  3.     int *tmp=px;
  4.     px=py;
  5.     py=tmp;
  6.     printf("*px=%d,*py=%d\n",*px,*py);
  7. }
复制代码

求助哥哥姐姐们,为什么第二个代码只是将形参的内容作了交换,指针变量前加不加星号各代表什么意思啊?
最佳答案
2020-12-12 17:00:11
  1. #include <stdio.h>

  2. // 指针 px、py 不变,将两个指针所指内存单元的内容互换
  3. void swap1(int * px , int * py)
  4. {
  5.    int tmp = * px                                  ;
  6.    * px = * py                                     ;
  7.    * py = tmp                                      ;
  8.     printf(" * px=%d , * py = %d\n" , * px , * py) ;
  9. }

  10. // 指针 px、py 互换,但两个指针所指内存单元的内容不变
  11. void swap2(int * px , int * py)
  12. {
  13.     int * tmp = px                                   ;
  14.     px = py                                          ;
  15.     py = tmp                                         ;
  16.     printf(" * px = %d , * py = %d\n" , * px , * py) ;
  17. }

  18. int main(void)
  19. {
  20.         int a = 3 , b = 3 , c = 5 , d = 5   ;
  21.         printf("a = %d , c = %d\n" , a , c) ;
  22.         swap1(& a , & c)                    ;
  23.         printf("a = %d , c = %d\n" , a , c) ;
  24.         printf("b = %d , d = %d\n" , b , d) ;
  25.         swap2(& b , & d)                    ;
  26.         printf("b = %d , d = %d\n" , b , d) ;
  27. }
复制代码

        swap() 传入的两个指针就好比传入了两个装有物品的箱子,箱子里面的物品是可以交换的,swap1() 做到了交换两个箱子中的物品,swap2() 只是交换了两个箱子,箱子中的物品并没有变,从函数内部观察,两个函数的交换效果是一样的,但是,从函数外部观察,效果就截然不同了,swap1() 有交换效果,而 swap2() 没有。

        编译、运行实况
  1. D:\00.Excise\C>cl x.c
  2. Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
  3. Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

  4. x.c
  5. Microsoft (R) Incremental Linker Version 6.00.8447
  6. Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

  7. /out:x.exe
  8. x.obj

  9. D:\00.Excise\C>x
  10. a = 3 , c = 5
  11. * px=5 , * py = 3
  12. a = 5 , c = 3
  13. b = 3 , d = 5
  14. * px = 5 , * py = 3
  15. b = 3 , d = 5

  16. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-12 17:00:11 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>

  2. // 指针 px、py 不变,将两个指针所指内存单元的内容互换
  3. void swap1(int * px , int * py)
  4. {
  5.    int tmp = * px                                  ;
  6.    * px = * py                                     ;
  7.    * py = tmp                                      ;
  8.     printf(" * px=%d , * py = %d\n" , * px , * py) ;
  9. }

  10. // 指针 px、py 互换,但两个指针所指内存单元的内容不变
  11. void swap2(int * px , int * py)
  12. {
  13.     int * tmp = px                                   ;
  14.     px = py                                          ;
  15.     py = tmp                                         ;
  16.     printf(" * px = %d , * py = %d\n" , * px , * py) ;
  17. }

  18. int main(void)
  19. {
  20.         int a = 3 , b = 3 , c = 5 , d = 5   ;
  21.         printf("a = %d , c = %d\n" , a , c) ;
  22.         swap1(& a , & c)                    ;
  23.         printf("a = %d , c = %d\n" , a , c) ;
  24.         printf("b = %d , d = %d\n" , b , d) ;
  25.         swap2(& b , & d)                    ;
  26.         printf("b = %d , d = %d\n" , b , d) ;
  27. }
复制代码

        swap() 传入的两个指针就好比传入了两个装有物品的箱子,箱子里面的物品是可以交换的,swap1() 做到了交换两个箱子中的物品,swap2() 只是交换了两个箱子,箱子中的物品并没有变,从函数内部观察,两个函数的交换效果是一样的,但是,从函数外部观察,效果就截然不同了,swap1() 有交换效果,而 swap2() 没有。

        编译、运行实况
  1. D:\00.Excise\C>cl x.c
  2. Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
  3. Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

  4. x.c
  5. Microsoft (R) Incremental Linker Version 6.00.8447
  6. Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

  7. /out:x.exe
  8. x.obj

  9. D:\00.Excise\C>x
  10. a = 3 , c = 5
  11. * px=5 , * py = 3
  12. a = 5 , c = 3
  13. b = 3 , d = 5
  14. * px = 5 , * py = 3
  15. b = 3 , d = 5

  16. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-8 17:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表