理解一般指標和指向指標的指標

來源:互聯網
上載者:User

標籤:

在說指向指標的指標之前,不得不說指向變數的指標。先看如下樣本:

1. 指向整型指標

先看如下樣本:

1 #include <iostream>using namespace std;int main(){ int a = 5; int * p = &a; cout << "a = " << a << endl << "&a = " << &a << endl << "*p = " << *p << endl << "p = " << p << endl << "&p = " << &p << endl; return 0;}

 

運行結果如下:

我們先看下記憶體配置圖:

由可以清楚的知道,輸出整形變數a的值是5,指標變數p的值是001BFD18,而*號的作用是取值,*p即取地址001BFD18中存放的值,即5。

2. 指向字元型指標

先看如下樣本:

1 #include <iostream>using namespace std;int main(){    char a[] = "hello";    char *p = a;    cout << "p = " << p << endl         <<"p = " << (void *) p << endl         << "*p = " << *p << endl;                    for(int i = 0 ; i < 5; i++)    {        cout << "&a[" << i << "] = "<< (void *)&a[i] << endl;    }    return 0;}

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     char a[] = "hello";
 7     char *p = a;
 8 
 9     cout << "p = " << p << endl
10          <<"p = " << (void *) p << endl
11          << "*p = " << *p << endl;
12         
13         
14     for(int i = 0 ; i < 5; i++)
15     {
16         cout << "&a[" << i << "] = "<< (void *)&a[i] << endl;
17     }
18     return 0;
19 }

運行結果圖如下:

為什麼整型指標p輸出的是地址,而字元型指標輸出的是字串呢,字元型指標裡存放的不是地址嗎?

我們先看下記憶體配置圖:

由可以看出,其實p中存放的是地址,只是當cout時,如果指標是字元型指標,那麼會輸出p中地址指向的記憶體中的內容(這裡是h)直到遇到‘\0‘才結束。所以直接輸出p時會輸出hello,而將p強制轉換為void *時輸出的是地址。

3. 指向整型指標的指標

先看如下樣本:

1 #include <iostream>using namespace std;int main(){    int a[5] = {1, 2, 3, 4, 5};    int *p = a;    int **point = &p;    cout << "a = " << a << endl        << "p = " << p << endl        << "&p = " << &p << endl        << "point = " << point << endl        << "&point = " << &point << endl;    for (int i = 0; i < 5; i++)    {        cout << "&a[" << i << "] = " << &a[i] << endl;    }    return 0;}

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int a[5] = {1, 2, 3, 4, 5};
 7     int *p = a;
 8     int **point = &p;
 9 
10     cout << "a = " << a << endl
11         << "p = " << p << endl
12         << "&p = " << &p << endl
13         << "point = " << point << endl
14         << "&point = " << &point << endl;
15 
16     for (int i = 0; i < 5; i++)
17     {
18         cout << "&a[" << i << "] = " << &a[i] << endl;
19     }
20     return 0;
21 }

運行結果圖如下:

我們先看下記憶體配置圖:

從可以看出point指標中存放的是p指標的地址,而p指標中存放的是a[0]的地址。所以*point和p是一樣的,前者是取point指標中存放的地址(0025F754)中的值,即取地址0025F754中存放的值(0025F760),而後者就是0025F760,所以兩者是等價的。**point和a[0]是等價的,前者可以寫成*p,*p是取p中存放的地址(0025F760)中的值,即地址0025F760中存放的值1。由上可以得出*point等於p, **point 等於 a[0]。通過可以清晰的對付諸如*point++等問題。

4. 指向整型指標的指標

先看如下樣本:

 

1 #include <iostream>using namespace std;int main(){    char *a[] = {"Wel", "To", "China"};    char **p = a;    for(int i = 0; i < 3; i++)    {        for (int j = 0; j < strlen(a[i]) + 1; j++)        {            cout << a[i][j] << "\t" << (void *)&a[i][j] << endl;        }        cout << endl;    }        for (int i = 0; i < 3; i++)    {        cout << "a[" << i << "] = " << (void *) a[i] << endl             << "&a[" << i << "] = " << &a[i] << endl;    }     cout << "p  = " << p << endl         << "&p = " << &p << endl;    return 0;}

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     char *a[] = {"Wel", "To", "China"};
 7     char **p = a;
 8     for(int i = 0; i < 3; i++)
 9     {
10         for (int j = 0; j < strlen(a[i]) + 1; j++)
11         {
12             cout << a[i][j] << "\t" << (void *)&a[i][j] << endl;
13         }
14         cout << endl;
15     }
16     
17     for (int i = 0; i < 3; i++)
18     {
19         cout << "a[" << i << "] = " << (void *) a[i] << endl
20              << "&a[" << i << "] = " << &a[i] << endl;
21     } 
22 
23 
24     cout << "p  = " << p << endl
25          << "&p = " << &p << endl;
26     return 0;
27 }

 

運行結果圖如下:

我們先看下記憶體配置圖:

由可以看出a[0]中存放著‘W‘的地址,a[1]中存放著‘T‘的地址,a[2]中存放著‘C‘的地址,只是這些地址都是指向字元型的,所以直接cout的會輸出字串,而指標p中存放著a[0]的地址,所以*p等於a[0],都是獲得‘W‘的地址,即00A778CCC,而**p和a[0][0]等價都獲得了地址00A778CCC中存放的值W。由我們可以看到字元地址相隔1個位元組,而指標地址相隔4個位元組,這樣就便於++運算,獲得下一個地址了,列如++p後,p就指向a[1],p中存放的是a[1]的地址。

理解一般指標和指向指標的指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.