<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-passing-array-to-function-example/](https://beginnersbook.com/2014/01/c-passing-array-to-function-example/) 就像變量一樣,數組也可以作為參數傳遞給函數。在本指南中,我們將學習如何使用按值調用和按引用調用方法將數組傳遞給函數。 > 要理解本指南,您應該具有以下 [C 編程](https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/)主題的知識: > > 1. [C - 數組](https://beginnersbook.com/2014/01/c-arrays-example/) > 2. [C 中的按值函數調用](https://beginnersbook.com/2014/01/c-function-call-by-value-example/) > 3. [C 中的按引用函數調用](https://beginnersbook.com/2014/01/c-function-call-by-reference-example/) ## 使用按值調用方法將數組傳遞給函數 正如我們在這種類型的函數調用中已經知道的那樣,實際參數被復制到形式參數中。 ```c #include <stdio.h> void disp( char ch) { printf("%c ", ch); } int main() { char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; for (int x=0; x<10; x++) { /* I’m passing each element one by one using subscript*/ disp (arr[x]); } return 0; } ``` **輸出:** ```c a b c d e f g h i j ``` ## 使用按引用調用將數組傳遞給函數 當我們在調用函數的同時傳遞數組的地址,然后這就是按引用函數調用。當我們傳遞一個地址作為參數時,函數聲明應該有一個[指針](https://beginnersbook.com/2014/01/c-pointers/)作為接收傳遞地址的參數。 ```c #include <stdio.h> void disp( int *num) { printf("%d ", *num); } int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; for (int i=0; i<10; i++) { /* Passing addresses of array elements*/ disp (&arr[i]); } return 0; } ``` 輸出: ```c 1 2 3 4 5 6 7 8 9 0 ``` ### 如何將整個數組作為參數傳遞給函數? 在上面的例子中,我們在 C 中使用[`for`循環](https://beginnersbook.com/2014/01/c-for-loop/)逐個傳遞每個數組元素的地址。但是,您也可以將整個數組傳遞給這樣的函數: > 注意:數組名稱本身是該數組的第一個元素的地址。例如,如果數組名稱為`arr`,則可以說`arr`等同于`&arr[0]` 。 ```c #include <stdio.h> void myfuncn( int *var1, int var2) { /* The pointer var1 is pointing to the first element of * the array and the var2 is the size of the array. In the * loop we are incrementing pointer so that it points to * the next element of the array on each increment. * */ for(int x=0; x<var2; x++) { printf("Value of var_arr[%d] is: %d \n", x, *var1); /*increment pointer for next element fetch*/ var1++; } } int main() { int var_arr[] = {11, 22, 33, 44, 55, 66, 77}; myfuncn(var_arr, 7); return 0; } ``` **輸出:** ```c Value of var_arr[0] is: 11 Value of var_arr[1] is: 22 Value of var_arr[2] is: 33 Value of var_arr[3] is: 44 Value of var_arr[4] is: 55 Value of var_arr[5] is: 66 Value of var_arr[6] is: 77 ```
                  <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>

                              哎呀哎呀视频在线观看