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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C++ 指針 > 原文: [https://www.programiz.com/cpp-programming/pointers](https://www.programiz.com/cpp-programming/pointers) #### 在本文中,您將學習有關指針的所有知識。 您將學習如何將值存儲在計算機中以及如何使用指針訪問它們。 指針是 C++ 的強大功能,可將其與其他編程語言(如 Java 和 Python)區分開來。 指針在 C++ 程序中用于訪問內存和操縱地址。 * * * ## C++ 地址 要了解指針,您首先應該知道如何在計算機上存儲數據。 您在程序中創建的每個變量都會在計算機的內存中分配一個位置。 變量存儲的值實際上存儲在分配的位置。 要知道數據存儲在哪里,C++ 有一個`&`運算符。 `&`(引用)運算符為您提供變量占用的地址。 如果`var`是變量,則`&var`給出該變量的地址。 ## 示例 1:C++ 中的地址 ```cpp #include <iostream> using namespace std; int main() { int var1 = 3; int var2 = 24; int var3 = 17; cout << &var1 << endl; cout << &var2 << endl; cout << &var3 << endl; } ``` **輸出** ```cpp 0x7fff5fbff8ac 0x7fff5fbff8a8 0x7fff5fbff8a4 ``` **注意**:您的系統上可能不會得到相同的結果。 開頭的`0x`表示地址為十六進制格式。 請注意,第一個地址與第二個地址相差 4 個字節,第二個地址與第三個地址相差 4 個字節。 這是因為在 64 位系統中,整數(類型為`int`的變量)的大小為 4 個字節。 * * * ## 指針變量 C++ 使您可以直接操作計算機內存中的數據。 您可以根據需要分配和取消分配內存中的任何空間。 這是使用指針變量完成的。 指針變量是指向另一個變量所指向的內存中特定地址的變量。 * * * ### 如何聲明一個指針? ```cpp int *p; OR, int* p; ``` 上面的語句定義了一個指針變量`p`。 它保存內存地址 星號是解引用運算符,表示**指向`p`的指針**。 此處,指針`p`是指向`int`的**指針**,即它指向內存地址中的整數值。 * * * ### 引用運算符(`&`)和解引用運算符(`*`) 如上所述的引用運算符(`&`)給出變量的地址。 要獲取存儲在內存地址中的值,我們使用解引用運算符(`*`)。 **例如**:如果`num`變量存儲在內存地址`0x123`中,并且它包含值 **5**。 **引用(`&`)**運算符給出值`0x123`,而**取消引用(`*`)**運算符給出值 **5**。 **注意**:在 C++ 指針的聲明中使用的(`*`)符號不是取消引用指針。 只是創建指針的類似符號。 * * * ## 示例 2:C++ 指針 **演示指針的工作的 C++ 程序。** ```cpp #include <iostream> using namespace std; int main() { int *pc, c; c = 5; cout << "Address of c (&c): " << &c << endl; cout << "Value of c (c): " << c << endl << endl; pc = &c; // Pointer pc holds the memory address of variable c cout << "Address that pointer pc holds (pc): "<< pc << endl; cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl; c = 11; // The content inside memory address &c is changed from 5 to 11. cout << "Address pointer pc holds (pc): " << pc << endl; cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl; *pc = 2; cout << "Address of c (&c): " << &c << endl; cout << "Value of c (c): " << c << endl << endl; return 0; } ``` **輸出** ```cpp Address of c (&c): 0x7fff5fbff80c Value of c (c): 5 Address that pointer pc holds (pc): 0x7fff5fbff80c Content of the address pointer pc holds (*pc): 5 Address pointer pc holds (pc): 0x7fff5fbff80c Content of the address pointer pc holds (*pc): 11 Address of c (&c): 0x7fff5fbff80c Value of c (c): 2 ``` ![Working of pointer in C++ programming](https://img.kancloud.cn/6f/d7/6fd7b1100ba562658fac13754c234d2d_450x355.png "C++ pointer and address") **程序**的說明 * 當`c = 5;`時,值 5 存儲在變量`c` - `0x7fff5fbff8c`的地址中。 * 當`pc = &c;`指針`pc`保持`c` - `0x7fff5fbff8c`的地址時,表達式(取消引用運算符)`*pc`輸出該地址中存儲的值,5。 * 由于地址指針`pc`保持與`c` - `0x7fff5fbff8c`相同,執行表達式`*pc`時也會反映`c`的值變化,輸出`c = 11;`。 * 當`*pc = 2;`時,它將更改`pc` - `0x7fff5fbff8c`存儲的地址的內容。 這從 11 更改為 2。因此,當我們打印`c`的值時,該值也為 2。 ## 使用指針時的常見錯誤 假設您希望指針`pc`指向`c`的地址。 然后, ```cpp int c, *pc; pc=c; /* Wrong! pc is address whereas, c is not an address. */ *pc=&c; /* Wrong! *pc is the value pointed by address whereas, %amp;c is an address. */ pc=&c; /* Correct! pc is an address and, %amp;pc is also an address. */ *pc=c; /* Correct! *pc is the value pointed by address and, c is also a value. */ ``` 在這兩種情況下,指針`pc`均不指向`c`的地址。 * * * 您還應該查看這些與指針相關的教程: * [如何使用`void`指針使用通用數據類型指針?](/cpp-programming/pointer-void "C++ pointer to void") * [如何使用指針表示數組?](/cpp-programming/pointers-arrays "Represent array using pointer") * [如何在函數中使用指針?](/cpp-programming/pointers-function "Use pointers with functions") * [如何在結構中使用指針?](/cpp-programming/structure-pointer "C++ use pointers with structures")
                  <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>

                              哎呀哎呀视频在线观看