fork download
  1. #include <stdio.h>
  2.  
  3. void swap(int *a, int *b) {
  4. int temp;
  5. temp = *a;
  6. *a = *b;
  7. *b = temp;
  8. }
  9.  
  10. int main(void) {
  11. int x = 10;
  12. int y = 20;
  13.  
  14. // swap関数の実行前
  15. printf("【swap実行前】\n");
  16. printf("xの値:%d, xのアドレス:%p\n", x, (void *)&x);
  17. printf("yの値:%d, yのアドレス:%p\n", y, (void *)&y);
  18.  
  19. // swap関数を実行
  20. swap(&x, &y);
  21.  
  22. // swap関数の実行後
  23. printf("\n【swap実行後】\n");
  24. printf("xの値:%d, xのアドレス:%p\n", x, (void *)&x);
  25. printf("yの値:%d, yのアドレス:%p\n", y, (void *)&y);
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
【swap実行前】
xの値:10, xのアドレス:0x7ffda66323d0
yの値:20, yのアドレス:0x7ffda66323d4

【swap実行後】
xの値:20, xのアドレス:0x7ffda66323d0
yの値:10, yのアドレス:0x7ffda66323d4