LoginSignup
0
3

More than 5 years have passed since last update.

[C/C++] 二つの変数を交換。

Last updated at Posted at 2017-06-23

C語の基本

交換

二つの変数を交換する

koukan.c

#include <stdio.h>
int main(void){

int nInput = 10;
int nResult = 20;
// init 必要。
int nTemp = 0;

nTemp = nInput;
nInput = nResult;
nResult = nTemp;

printf("%d, %d\n", nInput, nResult);

return 0;
}

重要な部分

koukan.c

nTemp = nInput;
nInput = nResult;
nResult = nTemp;

Function

一般関数。


void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

Pointer使う関数。


void swap_p(int *pa, int *pb) {
    int temp = *pa;
    *pa = *pb;
    *pb = temp;
}


他のよい方法あればコメントお願いします。

0
3
11

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3