文章目錄
- 目錄
- 1.指針
- 2.引用
目錄
1.指針
#include <iostream>using namespace std;int main ()
{int var1;char var2[10];cout << "var1 變量的地址: ";cout << &var1 << endl;cout << "var2 變量的地址: ";cout << &var2 << endl;return 0;
}
var1 變量的地址: 0x7ffd8696414c
var2 變量的地址: 0x7ffd86964150
#include <iostream>using namespace std;int main ()
{int var = 20; // 實際變量的聲明int *ip; // 指針變量的聲明ip = &var; // 在指針變量中存儲 var 的地址cout << "Value of var variable: ";cout << var << endl;// 輸出在指針變量中存儲的地址cout << "Address stored in ip variable: ";cout << ip << endl;// 訪問指針中地址的值cout << "Value of *ip variable: ";cout << *ip << endl;return 0;
}
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};int *ptr;// 指針中的數組地址ptr = var;for (int i = 0; i < MAX; i++){cout << "Address of var[" << i << "] = ";cout << ptr << endl;cout << "Value of var[" << i << "] = ";cout << *ptr << endl;// 移動到下一個位置ptr++;}return 0;
}
Address of var[0] = 0xbfa088b0
Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};int *ptr;// 指針中第一個元素的地址ptr = var;int i = 0;while ( ptr <= &var[MAX - 1] ){cout << "Address of var[" << i << "] = ";cout << ptr << endl;cout << "Value of var[" << i << "] = ";cout << *ptr << endl;// 指向上一個位置ptr++;i++;}return 0;
}
Address of var[0] = 0xbfce42d0
Value of var[0] = 10
Address of var[1] = 0xbfce42d4
Value of var[1] = 100
Address of var[2] = 0xbfce42d8
Value of var[2] = 200
然而,指針和數組并不是完全互換的。例如,請看下面的程序:
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};for (int i = 0; i < MAX; i++){*var = i; // 這是正確的語法var++; // 這是不正確的}return 0;
}
把指針運算符 * 應用到 var 上是完全可以的,但修改 var 的值是非法的。這是因為 var 是一個指向數組開頭的常量,不能作為左值。
由于一個數組名對應一個指針常量,只要不改變數組的值,仍然可以用指針形式的表達式。例如,下面是一個有效的語句,把 var[2] 賦值為 500:
*(var + 2) = 500;
上面的語句是有效的,且能成功編譯,因為 var 未改變
#include <iostream>using namespace std;
const int MAX = 3;int main ()
{int var[MAX] = {10, 100, 200};int *ptr[MAX];for (int i = 0; i < MAX; i++){ptr[i] = &var[i]; // 賦值為整數的地址}for (int i = 0; i < MAX; i++){cout << "Value of var[" << i << "] = ";cout << *ptr[i] << endl;}return 0;
}
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
#include <iostream>using namespace std;int main ()
{int var;int *ptr;int **pptr;var = 3000;// 獲取 var 的地址ptr = &var;// 使用運算符 & 獲取 ptr 的地址pptr = &ptr;// 使用 pptr 獲取值cout << "Value of var :" << var << endl;cout << "Value available at *ptr :" << *ptr << endl;cout << "Value available at **pptr :" << **pptr << endl;return 0;
}
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000
#include <iostream>
#include <ctime>using namespace std;
void getSeconds(unsigned long *par);int main ()
{unsigned long sec;getSeconds( &sec );// 輸出實際值cout << "Number of seconds :" << sec << endl;return 0;
}void getSeconds(unsigned long *par)
{// 獲取當前的秒數*par = time( NULL );return;
}
2.引用
#include <iostream>using namespace std;int main ()
{// 聲明簡單的變量int i;double d;// 聲明引用變量int& r = i;double& s = d;i = 5;cout << "Value of i : " << i << endl;cout << "Value of i reference : " << r << endl;d = 11.7;cout << "Value of d : " << d << endl;cout << "Value of d reference : " << s << endl;return 0;
}
Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7
#include <iostream>
using namespace std;// 函數聲明
void swap(int& x, int& y);int main ()
{// 局部變量聲明int a = 100;int b = 200;cout << "交換前,a 的值:" << a << endl;cout << "交換前,b 的值:" << b << endl;/* 調用函數來交換值 */swap(a, b);cout << "交換后,a 的值:" << a << endl;cout << "交換后,b 的值:" << b << endl;return 0;
}// 函數定義
void swap(int& x, int& y)
{int temp;temp = x; /* 保存地址 x 的值 */x = y; /* 把 y 賦值給 x */y = temp; /* 把 x 賦值給 y */return;
}
交換前,a 的值: 100
交換前,b 的值: 200
交換后,a 的值: 200
交換后,b 的值: 100
#include <iostream>using namespace std;double vals[] = {10.1, 12.6, 33.1, 24.1, 50.0};double& setValues( int i )
{return vals[i]; // 返回第 i 個元素的引用
}// 要調用上面定義函數的主函數
int main ()
{cout << "改變前的值" << endl;for ( int i = 0; i < 5; i++ ){cout << "vals[" << i << "] = ";cout << vals[i] << endl;}setValues(1) = 20.23; // 改變第 2 個元素setValues(3) = 70.8; // 改變第 4 個元素cout << "改變后的值" << endl;for ( int i = 0; i < 5; i++ ){cout << "vals[" << i << "] = ";cout << vals[i] << endl;}return 0;
}
改變前的值
vals[0] = 10.1
vals[1] = 12.6
vals[2] = 33.1
vals[3] = 24.1
vals[4] = 50
改變后的值
vals[0] = 10.1
vals[1] = 20.23
vals[2] = 33.1
vals[3] = 70.8
vals[4] = 50