C++指標理解

_Git發表於2018-05-27

整形陣列指標

void int__array_pointer_test() {
    int array[] = {1, 2, 3, 4, 5};
    int *p = array;
    int **pr = &p;
    for (int i = 0; i < 5; ++i) {
        cout << "array[" << i << "] address in memory is " << &array[i] << endl;
    }
    //輸出陣列名,輸出的是第一個元素的地址
    cout << "array = " << array << endl;
    //輸出指向整形陣列的指標,輸出的還是第一個元素的地址
    cout << "p = " << p << endl;
    //對指向整形陣列的指標做解引用操作,輸出的是第一個元素的值
    cout << "*p = " << *p << endl;
    //輸出指向整形陣列的指標的指標,輸出的是儲存著陣列第一個元素地址的記憶體地址
    cout << "pr = " << pr << endl;
    //輸出對pr做一次解引用操作的結果,也就是第一個元素的地址
    cout << "*pr = " << *pr << endl;
    //輸出對pr做兩次解引用操作的結果,也就是第一個元素的值
    cout << "**pr = " << **pr << endl;
    /**
     * 
     */
}
}
複製程式碼

執行結果:

array[0] address in memory is 0x28ff14
array[1] address in memory is 0x28ff18
array[2] address in memory is 0x28ff1c
array[3] address in memory is 0x28ff20
array[4] address in memory is 0x28ff24
array = 0x28ff14
p = 0x28ff14
*p = 1
pr = 0x28ff10
*pr = 0x28ff14
**pr = 1
複製程式碼

總結如下: 1. array[0] = *p = **pr; 2. array = p = *pr 3. pr存放的是p的地址,p存放的是array/array[0]的地址

字元陣列

void char_array_pointer_test(){
    char array[] = {"Digist"};
    char *p = array;
    char **pr = &p;
    for (int i = 0; i < 5; ++i) {
        cout << "array[" << i << "] address in memory is " << &array[i] << endl;
    }
    //輸出陣列名,輸出的是整個陣列的值
    cout << "array = " << array << endl;
    //輸出指向字元陣列的指標,輸出的也是整個陣列的值
    cout << "p = " << p << endl;
    //對字元陣列的指標做解引用操作,輸出的是第一個元素的值
    cout << "*p = " << *p << endl;
    //輸出指向字元陣列指標的指標,輸出的是儲存第一個元素地址的記憶體地址
    cout << "pr = " << pr << endl;
    //輸出對指向字元陣列的指標做一次解引用操作的結果,輸出的是整個陣列的值
    cout << "*pr = " << *pr << endl;
    //輸出對指向字元陣列的指標做兩次解引用操作的結果,輸出的是第一個元素的值
    cout << "**pr = " << **pr << endl;
}
}
複製程式碼

執行結果:

array[0] address in memory is Digist
array[1] address in memory is igist
array[2] address in memory is gist
array[3] address in memory is ist
array[4] address in memory is st
array = Digist
p = Digist
*p = D
pr = 0x28ff1c
*pr = Digist
**pr = D

複製程式碼

總結:發現字元陣列在輸出的時候和整形陣列有區別,本以為和整形陣列一樣應該輸出地址的地方基本都輸出的是整個陣列的值,而希望可以看到地址的話需要對指標進行強轉,轉成(int *)或者(void *)

修正結果

void char_array_pointer_test2(){
    char array[] = {"Digist"};
    char *p = array;
    char **pr = &p;
    for (int i = 0; i < 5; ++i) {
        cout << "array[" << i << "] address in memory is " << (int *)&array[i] << endl;
    }
    cout << "array = " << (void *)array << endl;
    cout << "p = " << (int *)p << endl;
    cout << "*p = " << *p << endl;
    cout << "&p = " << &p << endl;
    cout << "pr = " << pr << endl;
    cout << "*pr = " << *pr << endl;
    cout << "**pr = " << **pr << endl;
}
複製程式碼

執行結果:

array[0] address in memory is 0x28ff21
array[1] address in memory is 0x28ff22
array[2] address in memory is 0x28ff23
array[3] address in memory is 0x28ff24
array[4] address in memory is 0x28ff25
array = 0x28ff21
p = 0x28ff21
*p = D
&p = 0x28ff1c
pr = 0x28ff1c
*pr = Digist
**pr = D

複製程式碼

總結如下:

  1. 直接輸出字元陣列變數,輸出的是整個陣列的值,要輸出其地址,需要將變數名進行強轉
  2. 一個指向字元陣列的指標,輸出這個指標遍歷,輸出也是這個字元陣列的值,要得到其儲存的地址需要進行強轉
  3. &array[0] = array = p = *pr = 字元陣列的值; (int *)array = (void *)p = 字元陣列的記憶體地址;array[0] = *p = **pr = 首元素的值

結構體

struct my_year {
    int year;
};

void test_point_to_struct_pointer() {
    my_year s1, s2, s3;
    s1.year = 1998;
    s2.year = 2002;
    s3.year = 2004;
    my_year *pa = &s2;
    pa->year = 1999;
    my_year trio[3]{{1000},
                    {2000},
                    {3000}};
    my_year *pr = trio;
    my_year **ptr = &pr;
    for (int i = 0; i < 3; ++i) {
        cout << "trio[" << i << "] address in memory is " << &trio[i] << endl;
    }
    cout << "trio = " << trio << endl;
    cout << "pr = " << pr << endl;
    //此處會崩潰
    //cout << "*pr = " << *pr << endl;
    cout << "ptr = " << ptr << endl;
    cout << "*ptr = " << *ptr << endl;
    //cout << "**ptr = " << **ptr << endl;

    for (int i = 0; i < 3; ++i) {
        cout << (pr + i)->year << endl;
    }
    const my_year *arp[3] = {&s1, &s2, &s3};
    cout << "&s1 = " << &s1 << endl;
    cout << "&s2 = " << &s2 << endl;
    cout << "&s3 = " << &s3 << endl;

    const my_year **ppb = arp;
    cout << "ppb = " << ppb << endl;
    cout << "arp = " << arp << endl;
    cout << "arp[0] = " << arp[0] << endl;
    cout << "&arp[0] = " << &arp[0] << endl;
    cout << "*ppb = " << *ppb << endl;
    cout << "&ppb = " << &ppb << endl;
//    cout << (void *)arp << end;
}
複製程式碼

執行結果:

trio[0] address in memory is 0x28ff08
trio[1] address in memory is 0x28ff0c
trio[2] address in memory is 0x28ff10
trio = 0x28ff08
pr = 0x28ff08
ptr = 0x28ff04
*ptr = 0x28ff08
1000
2000
3000
&s1 = 0x28ff1c
&s2 = 0x28ff18
&s3 = 0x28ff14
ppb = 0x28fef8
arp = 0x28fef8
arp[0] = 0x28ff1c
&arp[0] = 0x28fef8
*ppb = 0x28ff1c
&ppb = 0x28fef4
複製程式碼

總結:結構體陣列中很多和整形陣列相同 trio = &trio[0] = pr = *ptr arp是一個指標陣列,arp = ppb = &arp[0],arp[0] = *ppb

指標陣列和陣列指標

指標陣列首先它是一個陣列,而陣列指標首先是一個指標 運算子的先後順序是:()>[]>* char *p[4]表示的是指標陣列 char (*p)[4]表示的是陣列指標 平時這樣寫

char array[4];
char *p;
p = array;
複製程式碼

相關文章