<分区>
我知道通过指针调用,我们在其中传递变量地址。像这样:
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */
return;
}
swap(&a, &b);
还有通过引用调用,在这两种方法中,函数中所做的更改都反射(reflect)在实际参数的变量中。
但是为什么在这种调用的情况下传递的实际参数没有改变:
#include <iostream>
using namespace std;
void foo(int* c){
c=c+1;
}
int main()
{
int a=5;
int *c=&a;
cout<<&c<<endl; //0x7ffe1a74f3b0
foo(c);
cout<<*c<<endl;//5
cout<<&c<<endl;//0x7ffe1a74f3b0
}
这里传递给 foo() 的 c 是 a 的地址。所以这是按值调用。
根据我的说法,这里 c 应该打印了垃圾值。请解释这里发生了什么。