<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://www.programiz.com/cpp-programming/pointers-arrays](https://www.programiz.com/cpp-programming/pointers-arrays) #### 在本文中,您將了解數組與指針之間的關系,并在程序中有效地使用它們。 [指針](/cpp-programming/pointers "C++ Pointers")是保存地址的變量。 指針不僅可以存儲單個變量的地址,還可以存儲[數組](/cpp-programming/arrays "C++ arrays")的單元格的地址。 考慮以下示例: ```cpp int* ptr; int a[5]; ptr = &a[2]; // &a[2] is the address of third element of a[5]. ``` ![Pointer pointing to an array cell](https://img.kancloud.cn/25/9c/259cae975f2959b64389f71eea2053b3_263x99.png "Pointers to array") 假設指針需要指向數組的第四個元素,即在上述情況下的第四個數組元素的保存地址。 由于在以上示例中`ptr`指向第三元素,所以`ptr + 1`將指向第四元素。 您可能會認為`ptr + 1`為您提供了`ptr`的下一個字節的地址。 但這是不正確的。 這是因為指針`ptr`是指向`int`的指針,并且`int`的大小對于操作系統是固定的(`int`的大小是 64 位操作系統的 4 字節)。 因此,`ptr`和`ptr + 1`之間的地址相差 4 個字節。 如果指針`ptr`是指向`char`的指針,則`ptr`和`ptr + 1`之間的地址將相差 1 個字節,因為字符的大小為 1 個字節。 * * * ## 示例 1:C++ 指針和數組 **C++ 程序,使用數組和指針顯示數組元素的地址** ```cpp #include <iostream> using namespace std; int main() { float arr[5]; float *ptr; cout << "Displaying address using arrays: " << endl; for (int i = 0; i < 5; ++i) { cout << "&arr[" << i << "] = " << &arr[i] << endl; } // ptr = &arr[0] ptr = arr; cout<<"\nDisplaying address using pointers: "<< endl; for (int i = 0; i < 5; ++i) { cout << "ptr + " << i << " = "<< ptr + i << endl; } return 0; } ``` **輸出** ```cpp Displaying address using arrays: &arr[0] = 0x7fff5fbff880 &arr[1] = 0x7fff5fbff884 &arr[2] = 0x7fff5fbff888 &arr[3] = 0x7fff5fbff88c &arr[4] = 0x7fff5fbff890 Displaying address using pointers: ptr + 0 = 0x7fff5fbff880 ptr + 1 = 0x7fff5fbff884 ptr + 2 = 0x7fff5fbff888 ptr + 3 = 0x7fff5fbff88c ptr + 4 = 0x7fff5fbff890 ``` 在上面的程序中,不同的指針`ptr`用于顯示數組元素`arr`的地址。 但是,可以使用相同的數組名稱`arr`使用指針符號訪問數組元素。 例如: ```cpp int arr[3]; &arr[0] is equivalent to arr &arr[1] is equivalent to arr + 1 &arr[2] is equivalen to arr + 2 ``` * * * ## 示例 2:指針和數組 **C++ 程序,用于使用指針符號顯示數組元素的地址。** ```cpp #include <iostream> using namespace std; int main() { float arr[5]; cout<<"Displaying address using pointers notation: "<< endl; for (int i = 0; i < 5; ++i) { cout << arr + i <<endl; } return 0; } ``` **輸出** ```cpp Displaying address using pointers notation: 0x7fff5fbff8a0 0x7fff5fbff8a4 0x7fff5fbff8a8 0x7fff5fbff8ac 0x7fff5fbff8b0 ``` 您知道,指針`ptr`保存地址,表達式`*ptr`給出存儲在地址中的值。 同樣,您可以使用`*(ptr + 1)`獲取存儲在指針`ptr + 1`中的值。 請考慮以下代碼: ```cpp int ptr[5] = {3, 4, 5, 5, 3}; ``` * `&ptr[0]`等于`ptr`,`*ptr`等于`ptr[0]` * `&ptr[1]`等于`ptr + 1`,`*(ptr + 1)`等于`ptr[1]` * `&ptr[2]`等于`ptr + 2`,`*(ptr + 2)`等于`ptr[2]` * `&ptr[i]`等于`ptr + i`,`*(ptr + i)`等于`ptr[i]` * * * ## 示例 3:C++ 指針和數組 **C++ 程序,用于插入和顯示使用指針符號輸入的數據。** ```cpp #include <iostream> using namespace std; int main() { float arr[5]; // Inserting data using pointer notation cout << "Enter 5 numbers: "; for (int i = 0; i < 5; ++i) { cin >> *(arr + i) ; } // Displaying data using pointer notation cout << "Displaying data: " << endl; for (int i = 0; i < 5; ++i) { cout << *(arr + i) << endl ; } return 0; } ``` **輸出** ```cpp Enter 5 numbers: 2.5 3.5 4.5 5 2 Displaying data: 2.5 3.5 4.5 5 2 ```
                  <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>

                              哎呀哎呀视频在线观看