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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 將指針傳遞給 C 中的函數 > 原文: [https://beginnersbook.com/2014/01/c-passing-pointers-to-functions/](https://beginnersbook.com/2014/01/c-passing-pointers-to-functions/) 在本教程中,您將學習如何將指針作為參數傳遞給函數。要理解這個概念,你必須對 C 編程中的[指針](https://beginnersbook.com/2014/01/c-pointers/)和[函數](https://beginnersbook.com/2014/01/c-functions-examples/)有基本的了解。 就像任何其他參數一樣,指針也可以作為參數傳遞給函數。讓我們舉一個例子來了解這是如何完成的。 ## 示例:在 C 編程中將指針傳遞給函數 在這個例子中,我們傳遞一個指向函數的指針。當我們將指針而不是變量作為參數傳遞時,則傳遞變量的地址而不是值。因此,使用指針的函數所做的任何更改,都是在傳遞的變量的地址處永久進行的。這種技術在 C 中稱為按引用調用。 > 嘗試沒有指針的同一個程序,你會發現獎金金額不會反映在工資中,這是因為函數所做的更改將對函數的局部變量進行。當我們使用指針時,值會在變量的地址處更改。 ```c #include <stdio.h> void salaryhike(int *var, int b) { *var = *var+b; } int main() { int salary=0, bonus=0; printf("Enter the employee current salary:"); scanf("%d", &salary); printf("Enter bonus:"); scanf("%d", &bonus); salaryhike(&salary, bonus); printf("Final salary: %d", salary); return 0; } ``` **輸出:** ```c Enter the employee current salary:10000 Enter bonus:2000 Final salary: 12000 ``` ## 示例 2:使用指針交換兩個數字 這是最流行的示例之一,顯示如何使用按引用調用交換數字。 > 嘗試沒有指針的程序,你會看到數字沒有交換。原因與我們在第一個例子中看到的相同。 ```c #include <stdio.h> void swapnum(int *num1, int *num2) { int tempnum; tempnum = *num1; *num1 = *num2; *num2 = tempnum; } int main( ) { int v1 = 11, v2 = 77 ; printf("Before swapping:"); printf("\nValue of v1 is: %d", v1); printf("\nValue of v2 is: %d", v2); /*calling swap function*/ swapnum( &v1, &v2 ); printf("\nAfter swapping:"); printf("\nValue of v1 is: %d", v1); printf("\nValue of v2 is: %d", v2); } ``` **輸出:** ```c Before swapping: Value of v1 is: 11 Value of v2 is: 77 After swapping: Value of v1 is: 77 Value of v2 is: 11 ```
                  <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>

                              哎呀哎呀视频在线观看