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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # C 編程中的二維(2D)數組 > 原文: [https://beginnersbook.com/2014/01/2d-arrays-in-c-example/](https://beginnersbook.com/2014/01/2d-arrays-in-c-example/) 數組數組稱為 2D 數組。[C 編程](https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/)中的二維(2D)數組也稱為矩陣。矩陣可以表示為行和列的表。在我們討論更多關于二維數組之前,讓我們來看看下面的 C 程序。 ## 簡單的二維(2D)數組示例 現在不用擔心如何初始化二維數組,我們稍后會討論該部分。該程序演示了如何將用戶輸入的元素存儲在二維數組中以及如何顯示二維數組的元素。 ```c #include<stdio.h> int main(){ /* 2D array declaration*/ int disp[2][3]; /*Counter variables for the loop*/ int i, j; for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("Enter value for disp[%d][%d]:", i, j); scanf("%d", &disp[i][j]); } } //Displaying array elements printf("Two Dimensional array elements:\n"); for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("%d ", disp[i][j]); if(j==2){ printf("\n"); } } } return 0; } ``` 輸出: ```c Enter value for disp[0][0]:1 Enter value for disp[0][1]:2 Enter value for disp[0][2]:3 Enter value for disp[1][0]:4 Enter value for disp[1][1]:5 Enter value for disp[1][2]:6 Two Dimensional array elements: 1 2 3 4 5 6 ``` ## 2D 數組的初始化 在聲明期間有兩種方法初始化二維數組。 ```c int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} }; ``` 要么 ```c int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17}; ``` 雖然上述兩個聲明都是有效的,但我建議您使用第一個方法,因為它更具可讀性,因為您可以在此方法中可視化 2d 數組的行和列。 #### 初始化 2D 數組時必須考慮的事項 我們已經知道,當我們在聲明期間初始化一個普通的[數組](https://beginnersbook.com/2014/01/c-arrays-example/)(或者你可以說是一維數組)時,我們不需要指定它的大小。但是,對于 2D 數組而言,情況并非如此,即使在聲明期間指定元素,也必須始終指定第二個維度。讓我們在幾個例子的幫助下理解這一點 - ```c /* Valid declaration*/ int abc[2][2] = {1, 2, 3 ,4 }? /* Valid declaration*/ int abc[][2] = {1, 2, 3 ,4 }? /* Invalid declaration – you must specify second dimension*/ int abc[][] = {1, 2, 3 ,4 }?? /* Invalid because of the same reason? mentioned above*/ int abc[2][] = {1, 2, 3 ,4 } ``` ## 如何將用戶輸入數據存儲到 2D 數組中 我們可以使用這個公式計算二維數組可以有多少元素: 數組`arr[n1][n2]`可以有`n1 * n2`個元素。我們在下面的示例中具有的數組具有尺寸 5 和 4。這些尺寸稱為下標。所以這個數組的**第一個下標**值為 5,**第二個下標**值為 4。 因此數組`abc[5][4]`可以有`5 * 4 = 20`個元素。 為了存儲用戶輸入的元素,我們使用兩個`for`循環,其中一個是嵌套循環。外循環從 0 到(第一個下標減一),內部`for`循環從 0 到(第二個下標減一)。這樣,用戶輸入元素的順序將是`abc[0][0]`,`abc[0][1]`,`abc[0][2]`......等等。 ```c #include<stdio.h> int main(){ /* 2D array declaration*/ int abc[5][4]; /*Counter variables for the loop*/ int i, j; for(i=0; i<5; i++) { for(j=0;j<4;j++) { printf("Enter value for abc[%d][%d]:", i, j); scanf("%d", &abc[i][j]); } } return 0; } ``` 在上面的例子中,我有一個整數類型的 2D 數組`abc`。從概念上講,你可以像這樣想象上面的數組: ![2D-array](https://img.kancloud.cn/05/8d/058dbebe1641f5c8fa5c9e93d873859a_500x500.jpg) 然而,這個數組在內存中的實際表示將是這樣的: ![memory-2D-diagram](https://img.kancloud.cn/ea/f3/eaf3f71de04753f7ee697bb5e965858e_550x350.jpg) ## 指針和二維數組 我們知道一維數組名稱作為指向數組的基本元素(第一個元素)的指針。然而,在 2D 數組的情況下,邏輯略有不同。您可以將 2D 數組視為多個一維數組的集合。 **所以** `abc[0]`將擁有第一行第一個元素的地址(如果我們考慮上面的圖表編號 1)。 類似地`abc[1]`將具有第二行的第一個元素的地址。為了更好地理解它,讓我們編寫一個 C 程序 - ```c #include <stdio.h> int main() { int abc[5][4] ={ {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}, {16,17,18,19} }; for (int i=0; i<=4; i++) { /* The correct way of displaying an address would be * printf("%p ",abc[i]); but for the demonstration * purpose I am displaying the address in int so that * you can relate the output with the diagram above that * shows how many bytes an int element uses and how they * are stored in contiguous memory locations. * */ printf("%d ",abc[i]); } return 0; } ``` 輸出: ```c 1600101376 1600101392 1600101408 1600101424 1600101440 ``` 實際地址表示應為十六進制,我們使用`%p`而不是`%d`,如注釋中所述。這只是為了表明元素存儲在連續的內存位置。您可以將輸出與上圖相關聯,以查看這些地址之間的差異實際上是該行元素消耗的字節數。 輸出中顯示的地址屬于每行的第一個元素`abc[0][0]`,`abc[1][0]`,`abc[2][0]`,`abc[3][0]`和`abc[4][0]`。
                  <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>

                              哎呀哎呀视频在线观看