# 指標與陣列

``#include <iostream> using namespace std; int main() {     int arr[10] = {0};     cout << "arr :\t\t" << arr          << endl;     cout << "&arr[0]: \t" << &arr[0]          << endl;     return 0; }``

arr :            0x22ff40
&arr[0]:     0x22ff40

``#include <iostream> using namespace std; int main() {    const int length = 10;    int arr[length] = {0};     int *ptr = arr;     for(int i = 0; i < length; i++) {         cout << "&arr[" << i << "]: " << &arr[i];         cout << "\tptr+" << i << ": " << ptr+i;         cout << endl;     }      return 0; }``

&arr[0]: 0x22ff30       ptr+0: 0x22ff30
&arr[1]: 0x22ff34       ptr+1: 0x22ff34
&arr[2]: 0x22ff38       ptr+2: 0x22ff38
&arr[3]: 0x22ff3c       ptr+3: 0x22ff3c
&arr[4]: 0x22ff40       ptr+4: 0x22ff40
&arr[5]: 0x22ff44       ptr+5: 0x22ff44
&arr[6]: 0x22ff48       ptr+6: 0x22ff48
&arr[7]: 0x22ff4c       ptr+7: 0x22ff4c
&arr[8]: 0x22ff50       ptr+8: 0x22ff50
&arr[9]: 0x22ff54       ptr+9: 0x22ff54

``#include <iostream> using namespace std; int main() {    const int length = 5;    int arr[length] = {10, 20, 30, 40, 50};     int *ptr = arr;     // 以指標方式存取資料     for(int i = 0; i < length; i++) {        cout << "*(ptr+" << i << "): " << *(ptr+i)             << endl;    }    cout << endl;    // 以陣列方式存取資料     for(int i = 0; i < length; i++) {        cout << "ptr[" << i << "]: " << ptr[i]             << endl;    }    cout << endl;    // 以指標方式存取資料     for(int i = 0; i < length; i++) {        cout << "*(arr+" << i << "): " << *(arr+i)             << endl;    }    cout << endl;     // 以陣列方式存取資料     for(int i = 0; i < length; i++) {        cout << "arr[" << i << "]: " << arr[i]             << endl;    }    cout << endl;     return 0; }``

*(ptr+0): 10
*(ptr+1): 20
*(ptr+2): 30
*(ptr+3): 40
*(ptr+4): 50

ptr[0]: 10
ptr[1]: 20
ptr[2]: 30
ptr[3]: 40
ptr[4]: 50

*(arr+0): 10
*(arr+1): 20
*(arr+2): 30
*(arr+3): 40
*(arr+4): 50

arr[0]: 10
arr[1]: 20
arr[2]: 30
arr[3]: 40
arr[4]: 50