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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 通過引用進行 C++ 調用:使用指針【包含示例】 > 原文: [https://www.programiz.com/cpp-programming/pointers-function](https://www.programiz.com/cpp-programming/pointers-function) #### 在本文中,您將學習將指針作為函數的參數傳遞,并在程序中有效地使用它。 在 [C++ 函數](/cpp-programming/function "C++ Functions")文章中,您了解了有關將參數傳遞給函數的知識。 因為傳遞了實際值,所以使用的此方法稱為按值傳遞。 但是,還有另一種方法將參數傳遞給不傳遞參數實際值的函數。 而是僅傳遞對該值的引用。 * * * ## 示例 1:不使用指針傳遞引用 ```cpp #include <iostream> using namespace std; // Function prototype void swap(int&, int&); int main() { int a = 1, b = 2; cout << "Before swapping" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; swap(a, b); cout << "\nAfter swapping" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; return 0; } void swap(int& n1, int& n2) { int temp; temp = n1; n1 = n2; n2 = temp; } ``` **輸出** ```cpp Before swapping a = 1 b = 2 After swapping a = 2 b = 1 ``` 在`main()`中,定義了兩個整數變量`a`和`b`。 這些整數通過引用傳遞給函數`swap()`。 編譯器可以識別出此引用是通過引用傳遞的,因為函數定義為`void swap(int& n1, int& n2)`(請在數據類型后注意`&`符號)。 在`swap()`函數中僅接收變量`a`和`b`的引用(地址),并且在變量的原始地址中進行交換。 在`swap()`函數中,`n1`和`n2`是形式參數,實際上分別與變量`a`和`b`相同。 還有另一種使用[指針](/cpp-programming/pointers "C++ Pointers")來完成相同任務的方法。 * * * ## 示例 2:使用指針傳遞引用 ```cpp #include <iostream> using namespace std; // Function prototype void swap(int*, int*); int main() { int a = 1, b = 2; cout << "Before swapping" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; swap(&a, &b); cout << "\nAfter swapping" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; return 0; } void swap(int* n1, int* n2) { int temp; temp = *n1; *n1 = *n2; *n2 = temp; } ``` 此示例的輸出與之前相同。 在這種情況下,變量的地址是在函數調用期間傳遞的,而不是變量本身。 ```cpp swap(&a, &b); // &a is address of a and &b is address of b ``` 由于傳遞的是地址而不是值,因此必須使用解引用運算符來訪問存儲在該地址中的值。 ```cpp void swap(int* n1, int* n2) { ... .. ... } ``` `*n1`和`*n2`分別給出存儲在地址`n1`和`n2`中的值。 由于`n1`包含`a`的地址,因此對`*n1`執行的任何操作也會更改`main()`函數中的`a`的值。 類似地,`b`將具有與`*n2`相同的值。
                  <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>

                              哎呀哎呀视频在线观看