<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C 編程中的指針和數組 > 原文: [https://beginnersbook.com/2014/01/c-pointer-to-array-example/](https://beginnersbook.com/2014/01/c-pointer-to-array-example/) 在本指南中,我們將學習如何在 C 程序中使用指針和數組。我建議你在閱讀本指南之前參考[數組](https://beginnersbook.com/2014/01/c-arrays-example/)和[指針](https://beginnersbook.com/2014/01/c-pointers/)教程,這樣你就可以很容易地理解這里解釋的概念了。 ## 打印數組元素地址的簡單示例 ```c #include <stdio.h> int main( ) { int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; /* for loop to print value and address of each element of array*/ for ( int i = 0 ; i < 7 ; i++ ) { /* The correct way of displaying the address would be using %p format * specifier like this: * printf("val[%d]: value is %d and address is %p\n", i, val[i], &val[i]); * Just to demonstrate that the array elements are stored in contiguous * locations, I m displaying the addresses in integer */ printf("val[%d]: value is %d and address is %d\n", i, val[i], &val[i]); } return 0; } ``` **輸出:** ```c val[0]: value is 11 and address is 1423453232 val[1]: value is 22 and address is 1423453236 val[2]: value is 33 and address is 1423453240 val[3]: value is 44 and address is 1423453244 val[4]: value is 55 and address is 1423453248 val[5]: value is 66 and address is 1423453252 val[6]: value is 77 and address is 1423453256 ``` > 請注意,每個元素之間存在 4 個字節的差異,因為這是整數的大小。這意味著所有元素都存儲在內存中的連續位置。(參見下圖) ![Pointer-to-array](https://img.kancloud.cn/9b/25/9b25a2cba49c86c1427289ab2f4af4a8_550x300.jpg) 在上面的例子中,我使用`&val[i]`來獲取數組的第`i`個元素的地址。我們也可以使用指針變量而不是使用`&`符號來獲取地址。 ## 示例 - C 中的數組和指針示例 ```c #include <stdio.h> int main( ) { /*Pointer variable*/ int *p; /*Array declaration*/ int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; /* Assigning the address of val[0] the pointer * You can also write like this: * p = var; * because array name represents the address of the first element */ p = &val[0]; for ( int i = 0 ; i<7 ; i++ ) { printf("val[%d]: value is %d and address is %p\n", i, *p, p); /* Incrementing the pointer so that it points to next element * on every increment. */ p++; } return 0; } ``` 輸出: ```c val[0]: value is 11 and address is 0x7fff51472c30 val[1]: value is 22 and address is 0x7fff51472c34 val[2]: value is 33 and address is 0x7fff51472c38 val[3]: value is 44 and address is 0x7fff51472c3c val[4]: value is 55 and address is 0x7fff51472c40 val[5]: value is 66 and address is 0x7fff51472c44 val[6]: value is 77 and address is 0x7fff51472c48 ``` **注意事項:** 1)使用數組和指針時,指針的數據類型必須與數組的數據類型匹配。 2)你也可以使用數組名來初始化指針,如下所示: ```c p = var; ``` 因為數組名稱本身就等于數組的基址。 ```c val==&val[0]; ``` 3)在循環中,對指針變量執行遞增操作(`p++`)以獲取下一個位置(下一個元素的位置),此算法對于所有類型的數組都是相同的(對于所有數據類型`double`,`char`,`int`等)即使每種數據類型消耗的字節不同。 **指針邏輯** 你必須已經理解了上面代碼中的邏輯,所以現在是時候玩幾個指針算術和表達式了。 ```c if p = &val[0] which means *p ==val[0] (p+1) == &val[2] ?& *(p+1) == val[2] (p+2) == &val[3] ?& *(p+2) == val[3] (p+n) == &val[n+1) & *(p+n) == val[n+1] ``` 使用這個邏輯,我們可以用更好的方式重寫我們的代碼: ```c #include <stdio.h> int main( ) { int *p; int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ; p = val; for ( int i = 0 ; i<7 ; i++ ) { printf("val[%d]: value is %d and address is %p\n", i, *(p+i), (p+i)); } return 0; } ``` 我們在這個程序中不需要`p++`語句。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看