ポインタを使用していないことに注意してください。しかし、この関数を使用すると、コードブロックが終了したときに値が通常に戻ります。スワップ関数 - ポインタ - 混乱
しかし、コードは私が実際にポインタで得る答えでコンパイルしています。
私はポインタに関連するファウルコンセプトを持っていると混乱しているので、私は助けが必要です。
void swap(int i, int j) {
int temp = i;
i = j;
j = temp;
}
int main() {
int a = 110;
int b = 786;
cout << "Before swapping the value" << endl;
cout << "'a' stores the value : " << a << endl;
cout << "'b' stores the value : " << b << endl;
swap(a,b);
cout << "\nAfter swapping the value" << endl;
cout << "'a' stores the value : " << a << endl;
cout << "'b' stores the value : " << b << endl;
swap(a, b);
cout << "\nAnd back again swapping the value" << endl;
cout << "'a' stores the value : " << a << endl;
cout << "'b' stores the value : " << b << endl;
return 0;
}
I am getting results without using pointers - is this IDE problem
でプログラムの実際の出力とは何ですか?どのようなアウトプットを期待しましたか?あなたの質問を編集してください。 –
あなたがあなたのargをコピーし、それらに新しい価値を割り当てないという意味で渡しているように、これはうまくいかないので、 – EdChum
おそらく、マクロを使ってスワップしようとしています: '#define swap(i、j)do {int temp = i; i = j; while(0) ' – GPS