<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 數組和指針之間的關系 > 原文: [https://www.programiz.com/c-programming/c-pointers-arrays](https://www.programiz.com/c-programming/c-pointers-arrays) #### 在本教程中,您將了解 C 編程中數組與指針之間的關系。 您還將學習使用指針訪問數組元素。 在了解數組與指針之間的關系之前,請確保檢查以下兩個主題: * [C 數組](/c-programming/c-arrays "C arrays") * [C 指針](/c-programming/c-pointers "C pointers") * * * ## 數組和指針之間的關系 數組是一個順序數據塊。 讓我們編寫一個程序來打印數組元素的地址。 ```c #include <stdio.h> int main() { int x[4]; int i; for(i = 0; i < 4; ++i) { printf("&x[%d] = %p\n", i, &x[i]); } printf("Address of array x: %p", x); return 0; } ``` **輸出** ```c &x[0] = 1450734448 &x[1] = 1450734452 &x[2] = 1450734456 &x[3] = 1450734460 Address of array x: 1450734448 ``` 數組`x`的兩個連續元素之間相差 4 個字節。 這是因為`int`的大小為 4 個字節(在我們的編譯器中)。 注意,`& x [0]`和`x`的地址相同。 這是因為變量名`x`指向數組的第一個元素。 ![Relation between arrays and pointers](https://img.kancloud.cn/4a/64/4a6474cca783e1c86a13ee0acce7bfbe_263x73.png "Arrays as Pointers") 從上面的示例中,很明顯`&x[0]`等于`x`。 并且,`x[0]`等效于`*x`。 同樣, * `&x[1]`等效于`x+1`,`x[1]`等效于`*(x+1)`。 * `&x[2]`等效于`x+2`,`x[2]`等效于`*(x+2)`。 * ... * 基本上,`&x[i]`等效于`x+i`,`x[i]`等效于`*(x+i)`。 * * * ### 示例 1:指針和數組 ```c #include <stdio.h> int main() { int i, x[6], sum = 0; printf("Enter 6 numbers: "); for(i = 0; i < 6; ++i) { // Equivalent to scanf("%d", &x[i]); scanf("%d", x+i); // Equivalent to sum += x[i] sum += *(x+i); } printf("Sum = %d", sum); return 0; } ``` 運行該程序時,輸出為: ```c Enter 6 numbers: 2 3 4 4 12 4 Sum = 29 ``` 在這里,我們聲明了一個由 6 個元素組成的數組`x`。 為了訪問數組的元素,我們使用了指針。 * * * 在大多數情況下,數組名稱會衰減為指針。 簡而言之,數組名稱將轉換為指針。 這就是為什么您可以使用指針訪問數組元素的原因。 但是,您應該記住**指針和數組不是相同的**。 在某些情況下,數組名稱不會衰減到指針。 要了解更多信息,請訪問:[數組名稱何時不衰減為指針?](https://stackoverflow.com/questions/17752978/exceptions-to-array-decaying-into-a-pointer) * * * ### 示例 2:數組和指針 ```c #include <stdio.h> int main() { int x[5] = {1, 2, 3, 4, 5}; int* ptr; // ptr is assigned the address of the third element ptr = &x[2]; printf("*ptr = %d \n", *ptr); // 3 printf("*(ptr+1) = %d \n", *(ptr+1)); // 4 printf("*(ptr-1) = %d", *(ptr-1)); // 2 return 0; } ``` 運行該程序時,輸出為: ```c *ptr = 3 *(ptr+1) = 4 *(ptr-1) = 2 ``` 在此示例中,第三個元素的地址`&x[2]`被分配給`ptr`指針。 因此,當我們打印`*ptr`時顯示`3`。 并且,打印`*(ptr+1)`給我們第四個元素。 同樣,打印`*(ptr-1)`提供了第二個要素。
                  <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>

                              哎呀哎呀视频在线观看