<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 編程中的數組 > 原文: [https://beginnersbook.com/2014/01/c-arrays-example/](https://beginnersbook.com/2014/01/c-arrays-example/) 數組是相同數據類型的分組(或集合)。例如,`int`數組包含`int`類型的元素,而`float`數組包含`float`類型的元素。 ## 為什么我們需要在 C 編程中使用數組? 考慮一種情況,您需要找出用戶輸入的 100 個整數的平均值。在 C 中,您有兩種方法可以執行此操作:1)使用`int`數據類型定義 100 個變量,然后執行 100 次`scanf()`操作以將輸入的值存儲在變量中,然后最后計算它們的平均值。 2)使用單個整數數組來存儲所有值,循環數組以將所有輸入的值存儲在數組中,然后計算平均值。 **依你來看哪種解決方案更好?** 顯然是第二種解決方案,將相同的數據類型存儲在一個變量中以及稍后使用數組索引訪問它們很方便(我們將在本教程后面討論)。 ### 如何在 C 中聲明數組 ```c int num[35];? /* An integer array of 35 elements */ char ch[10];? /* An array of characters for 10 elements */ ``` 類似地,數組可以是任何數據類型,例如`double`,`float`,`short`等。 ### 如何在 C 中訪問數組的元素 您可以使用**數組下標**(或索引)來訪問存儲在數組中的任何元素。下標從 0 開始,這意味著`arr[0]`代表數組`arr`中的第一個元素。 通常,`arr[n-1]`可用于訪問數組的第`n`個元素。其中`n`是任何整數。 **例如:** ```c int mydata[20]; mydata[0] /* first element of array mydata*/ mydata[19] /* last (20th) element of array mydata*/ ``` ### 數組示例:在 C 編程中找出 4 個整數的平均值 ```c #include <stdio.h> int main() { int avg = 0; int sum =0; int x=0; /* Array- declaration – length 4*/ int num[4]; /* We are using a for loop to traverse through the array * while storing the entered values in the array */ for (x=0; x<4;x++) { printf("Enter number %d \n", (x+1)); scanf("%d", &num[x]); } for (x=0; x<4;x++) { sum = sum+num[x]; } avg = sum/4; printf("Average of entered number is: %d", avg); return 0; } ``` 輸出: ```c Enter number 1 10 Enter number 2 10 Enter number 3 20 Enter number 4 40 Average of entered number is: 20 ``` 讓我們討論上述程序的重要部分: #### 將數據輸入數組 這里我們**將數組**從 0 迭代到 3,因為數組的大小是 4。在循環內部,我們向用戶顯示一條消息以輸入值。使用`scanf`函數將所有輸入值存儲在相應的數組元素中。 ```c for (x=0; x<4;x++) { printf("Enter number %d \n", (x+1)); scanf("%d", &num[x]); } ``` #### 從數組中讀出數據 假設,如果我們想要顯示數組的元素,那么我們就可以在 C 中使用[`for`循環](https://beginnersbook.com/2014/01/c-for-loop/)。 ```c for (x=0; x<4;x++) { printf("num[%d]\n", num[x]); } ``` ### 各種初始化數組的方法 在上面的例子中,我們剛剛聲明了數組,然后我們用用戶輸入的值初始化它。但是,您也可以在聲明期間初始化數組,如下所示: ```c int arr[5] = {1, 2, 3, 4 ,5}; ``` 或(兩者都相同) ```c int arr[] = {1, 2, 3, 4, 5}; ``` 未初始化的數組始終包含垃圾值。 ## C 數組 - 內存表示 ![c-arrays](https://img.kancloud.cn/9b/25/9b25a2cba49c86c1427289ab2f4af4a8_550x300.jpg) **關于 C 中數組的更多主題:** [**2D 數組**](https://beginnersbook.com/2014/01/2d-arrays-in-c-example/) - 我們可以在 C 中使用多維數組,如 2D 和 3D 數組。然而,最流行和最常用的數組是 2D - 二維數組。在這篇文章中,您將學習如何在 2D 數組中聲明,讀取和寫入數據以及它的各種其他功能。 [**將數組傳遞給函數**](https://beginnersbook.com/2014/01/c-passing-array-to-function-example/) - 通常我們在調用函數時傳遞值和變量,同樣我們也可以將數組傳遞給函數。您可以將數組的元素以及整個數組(通過僅指定數組名稱,它用作指針)傳遞給函數。 [**指向數組的指針**](https://beginnersbook.com/2014/01/c-pointer-to-array-example/) - 可以使用 C 中的[指針](https://beginnersbook.com/2014/01/c-pointers/)訪問和操作數組元素。使用指針可以輕松處理數組。只需將數組的基址分配給指針變量,就可以訪問數組的所有元素。
                  <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>

                              哎呀哎呀视频在线观看