整理一下指针与引用。 // 输出为 // 1 2 // 1 2 // 2 1 // 1 2 #include <iostream> using namespace std; void swap1( int x, int y ) { int temp; temp = x; x = y; y = temp; } void swap2( int &x, int &y ) { int temp; temp = x; x = y; y = temp; } void swap3( int* x, int* y) { int temp; temp = *x; *x = *y; *y = temp; } int main() { int a = 1, b = 2; cout << a << " " << b << endl; swap1(a, b); // swap 不成功 cout << a << " " << b << endl; swap2(a, b); // swap 成功 cout << a << " " << b << endl; swap3(&a, &b); // swap 成功 cout << a << " " << b << endl; system( "pause" ); return 0; } 再举一个经典例子: #include <iostream> // 错误。传的是形参。 void GetMemory1 ( char* p, int num ) { p = (char*)malloc( sizeof(char) * num ); } // 正确。用指针的指针实现 void GetMemory2 ( char** p, int num ) { *p = (char*)malloc( sizeof(char) * num ); } // 正确。用返回值实现 char* GetMemory3 ( char* p, int num ) { p = (char*)malloc( sizeof(char) * num ); return p; } int main() { char* str = NULL; GetMemory1( str, 100 ); strcpy( str, "hello" ); GetMemory2( &str, 100 ); strcpy( str, "hello" ); str = GetMemory3( str, 100 ); strcpy( str, "hello" ); system( "pause" ); return 0; }