學點 C 語言(22): 資料類型 – 多維陣列與指標

來源:互聯網
上載者:User
1. 關於數組的首地址:
#include <stdio.h>int main(void){    char cs[2][3] = {                      {'A','B','C'},                      {'D','E','F'}                    };    char *p1,*p2,*p3,*p4;    p1 = p2 = p3 = p4 = NULL;    /* 下面四個指標都是指向了同一個地址 */    p1 = &cs[0][0]; /* 這個最好理解 */    p2 = &cs[0];    p3 = &cs;    p4 = cs;        /* 這個最方便   */    printf("%p\n%p\n%p\n%p\n", p1, p2, p3, p4);    /* 顯示地址 */    printf("\n%c %c %c %c\n", *p1, *p2, *p3, *p4); /* 顯示內容 */    getchar();    return 0;}

2. 數組其他元素的地址:

例子中, 數組的元素在記憶體中應該是這樣排列的:

[0][0] [0][1] [0][2] [1][0] [1][1] [1][2]

下面是通過指標的方式擷取數組的第三個元素:

#include <stdio.h>int main(void){    int nums[2][3] = {                       {11, 12, 13},                       {21, 22, 23}                     };    int *p1,*p2;    p1 = p2 = NULL;    p1 = &nums[0][2];        p2 = nums;    p2 = p2 + 2;//    p2 = (int *)nums + 2; /* 或者用這一句替換上面兩行 */    printf("%d, %d\n", *p1, *p2);    getchar();    return 0;}

3. 遍曆數組的普通方法:

#include <stdio.h>int main(void){    int nums[2][3] = {                       {11, 12, 13},                       {21, 22, 23}                     };    int i,j;    for (i = 0; i 

4. 通過指標遍曆數組:

#include <stdio.h>int main(void){    int nums[2][3] = {                       {11, 12, 13},                       {21, 22, 23}                     };    int *p = nums;        int i;    for (i = 0; i 
#include <stdio.h>int main(void){    char cs[2][3] = {                      {'A','B','C'},                      {'D','E','F'}                    };    char *p = cs;        unsigned i;        for (i = 0; i 
#include <stdio.h>int main(void){    char cs[2][3] = {                      {'A','B','C'},                      {'D','E','F'}                    };    int i;    for (i = 0; i 

5. 再探數組的指標地址:

#include <stdio.h>int main(void){    char cs[2][3] = {                      {'A','B','C'},                      {'D','E','F'}                    };    //在本例中(二維數組)    // cs 是指向數組 cs[0] 的地址    // *cs 是指向 cs[0][0] 的地址    printf("%p, %p\n", cs, *cs);        // **cs 指向 cs[0][0] 的值    printf("%c, %c\n", **cs, cs[0][0]);       getchar();    return 0;}

6. 使用指標遍曆三維數組:

#include <stdio.h>int main(void){    char cs[2][2][3] = {                         {                           {'A','B','C'},                           {'D','E','F'}                         },                         {                           {'1','2','3'},                           {'4','5','6'}                         }                       };    int i;    int count = sizeof cs / sizeof cs[0][0][0];    for (i = 0; i 

7. 遍曆多維陣列還是用指標變數更容易理解:

#include <stdio.h>int main(void){    char cs[2][2][3] = {                         {                           {'A','B','C'},                           {'D','E','F'}                         },                         {                           {'1','2','3'},                           {'4','5','6'}                         }                       };    char *p = (char *)cs;  /* 相對上面的例子, 這裡加了類型轉換; 這樣編譯器就沒有提示了 */                           int i;    int count = sizeof cs / sizeof cs[0][0][0];    for (i = 0; i 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.