每周小題–函數傳值

  • 2019 年 11 月 24 日
  • 筆記

來源:公眾號【編程珠璣】

作者:守望先生

ID:shouwangxiansheng

下面程式碼輸出結果是什麼?

//來源:公眾號【編程珠璣】#include<stdio.h>  void swap_int(int a , int b)  {      int temp = a;      a = b;      b = temp;  }  void swap_str(char* a , char* b)  {      char* temp = a;      a = b;      b = temp;  }    int main(void)  {      int a = 10;      int b = 5;      char* str_a = "hello world";      char* str_b = "world hello";        swap_int(a , b);      swap_str(str_a , str_b);      printf("%d,%dn",a,b);      printf("%s,%sn",str_a,str_b);      return 0;  }

A:

5,10  hello world,world hello  

B:

5,10  world hello,hello world

C:

10,5  world hello,hello world 

D:

10,5  hello world,world hello  

如果要交換a和b的內容,以及str_a和str_b指向的字元串,應該如何修改?

本文問題答案可在《函數參數的傳值和傳指針有什麼區別?》中尋找並加深理解。