fork download
  1. #include <stdio.h>
  2.  
  3. void swap(int *x, int *y) {
  4. int w;
  5. w = *x;
  6. *x = *y;
  7. *y = w;
  8. }
  9.  
  10. int main() {
  11. int data1[5] = {1, 2, 3, 4, 5};
  12. int data2[5] = {6, 7, 8, 9, 10};
  13.  
  14. printf("入れ替え前:\n");
  15. printf("data1: ");
  16. for (int i = 0; i < 5; i++) {
  17. printf("%d ", data1[i]);
  18. }
  19. printf("data2: ");
  20. for (int i = 0; i < 5; i++) {
  21. printf("%d ", data2[i]);
  22. }
  23. printf("\n");
  24.  
  25. for (int i = 0; i < 5; i++) {
  26. swap(&data1[i], &data2[i]);
  27. }
  28.  
  29. printf("入れ替え後:\n");
  30. printf("data1: ");
  31. for (int i = 0; i < 5; i++) {
  32. printf("%d ", data1[i]);
  33. }
  34. printf("data2: ");
  35. for (int i = 0; i < 5; i++) {
  36. printf("%d ", data2[i]);
  37. }
  38. printf("\n");
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
入れ替え前:
data1: 1 2 3 4 5 data2: 6 7 8 9 10 
入れ替え後:
data1: 6 7 8 9 10 data2: 1 2 3 4 5