注意在代碼中我沒有使用指針,但我有一些概念,如果我要使用這個函數,當代碼塊完成時,該值將返回到正常值。交換函數 - 指針 - 混淆
但代碼編譯與實際上會用指針得到的答案。
我需要幫助,因爲我很困惑,如果我有與指針有關的犯規概念。
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
什麼是你的程序的實際輸出?你期望輸出什麼?請修改您的問題以包含該問題。 –
對不起,你聲稱''交換'工作或不工作,因爲這是行不通的:http://ideone.com/xzGMcl,因爲你傳遞的價值意味着它複製你的參數,並沒有給他們分配任何新的價值 – EdChum
也許嘗試與宏交換:'#define swap(i,j)do {int temp = i; i = j; j = temp;} while(0)' – GPS