<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://www.programiz.com/c-programming/c-arrays-functions](https://www.programiz.com/c-programming/c-arrays-functions) #### 在本教程中,您將學習通過示例將數組(一維和多維數組)傳遞給 C 編程中的函數的方法。 在 C 編程中,您可以將整個數組傳遞給函數。 在了解這一點之前,讓我們看看如何將數組的各個元素傳遞給函數。 * * * ### 傳遞單個數組元素 將數組元素傳遞給函數類似于[將變量傳遞給函數](/c-programming/c-user-defined-functions "C Functions")。 * * * ### 示例 1:傳遞數組 ```c #include <stdio.h> void display(int age1, int age2) { printf("%d\n", age1); printf("%d\n", age2); } int main() { int ageArray[] = {2, 8, 4, 12}; // Passing second and third elements to display() display(ageArray[1], ageArray[2]); return 0; } ``` **輸出** ```c 8 4 ``` * * * ### 示例 2:將數組傳遞給函數 ```c // Program to calculate the sum of array elements by passing to a function #include <stdio.h> float calculateSum(float age[]); int main() { float result, age[] = {23.4, 55, 22.6, 3, 40.5, 18}; // age array is passed to calculateSum() result = calculateSum(age); printf("Result = %.2f", result); return 0; } float calculateSum(float age[]) { float sum = 0.0; for (int i = 0; i < 6; ++i) { sum += age[i]; } return sum; } ``` **輸出** ```c Result = 162.50 ``` 要將整個數組傳遞給函數,僅將數組名作為參數傳遞。 ```c result = calculateSum(age); ``` 但是,請注意在函數定義中使用了`[]`。 ```c float calculateSum(float age[]) { ... .. } ``` 這將通知編譯器您正在將一維數組傳遞給該函數。 * * * ## 將多維數組傳遞給函數 要將多維數組傳遞給函數,僅將數組的名稱傳遞給函數(類似于一維數組)。 ### 示例 3:傳遞二維數組 ```c #include <stdio.h> void displayNumbers(int num[2][2]); int main() { int num[2][2]; printf("Enter 4 numbers:\n"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) scanf("%d", &num[i][j]); // passing multi-dimensional array to a function displayNumbers(num); return 0; } void displayNumbers(int num[2][2]) { printf("Displaying:\n"); for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { printf("%d\n", num[i][j]); } } } ``` **輸出** ```c Enter 4 numbers: 2 3 4 5 Displaying: 2 3 4 5 ``` **注意**:在 C 編程中,可以將數組傳遞給函數,但是,不能從函數返回數組。
                  <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>

                              哎呀哎呀视频在线观看