注意在代码中我没有使用指针,但我有一些概念,如果我要使用这个函数,当代码块完成时,该值将返回到正常值。交换函数 - 指针 - 混淆
但代码编译与实际上会用指针得到的答案。
我需要帮助,因为我很困惑,如果我有与指针有关的犯规概念。
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