我对 C++ 很陌生,而且我刚刚开始使用指针,因此当我使用指针进行引用调用时,当我传递值的地址时我无法理解一件事,它应该按应有的方式工作
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main(){
int x=5 , y=10;
swap(&x , &y);
std::cout<<"The value of x is "<<x<<std::endl;
std::cout<<"The value of y is "<<y<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是当我传递值而不是地址时,它仍然如何工作?
int main(){
int x=5 , y=10;
swap(x , y);
std::cout<<"The value of x is "<<x<<std::endl;
std::cout<<"The value of y is "<<y<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)