<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/2017/08/cpp-pointers/](https://beginnersbook.com/2017/08/cpp-pointers/) 指針是 C++ 中的一個變量,它包含另一個變量的地址。它們的[數據類型](https://beginnersbook.com/2017/08/cpp-data-types/)就像變量一樣,例如整數類型指針可以保存整數變量的地址,字符類型指針可以保存`char`變量的地址。 #### 指針的語法 ```cpp data_type *pointer_name; ``` **如何聲明指針?** ```cpp /* This pointer p can hold the address of an integer * variable, here p is a pointer and var is just a * simple integer variable */ int *p, var ``` **賦值** 如上所述,整數類型指針可以保存另一個`int`變量的地址。這里我們有一個整數變量`var`和指針`p`,它保存`var`的地址。要將變量的地址賦值給指針,我們使用**`&`符號**。 ```cpp /* This is how you assign the address of another variable * to the pointer */ p = &var; ``` **如何使用它?** ```cpp // This will print the address of variable var cout<<&var; /* This will also print the address of variable * var because the pointer p holds the address of var */ cout<<p; /* This will print the value of var, This is * important, this is how we access the value of * variable through pointer */ cout<<*p; ``` ## 指針示例 讓我們舉一個簡單的例子來理解我們上面討論的內容。 ```cpp #include <iostream> using namespace std; int main(){ //Pointer declaration int *p, var=101; //Assignment p = &var; cout<<"Address of var: "<<&var<<endl; cout<<"Address of var: "<<p<<endl; cout<<"Address of p: "<<&p<<endl; cout<<"Value of var: "<<*p; return 0; } ``` **輸出:** ```cpp Address of var: 0x7fff5dfffc0c Address of var: 0x7fff5dfffc0c Address of p: 0x7fff5dfffc10 Value of var: 101 ``` ## 指針和數組 在使用指針處理[數組](https://beginnersbook.com/2017/08/cpp-arrays/)時,您需要注意一些事情。關于數組的第一個也是非常重要的注意事項是,數組名稱單獨表示數組的基地址,因此在將數組地址賦值給指針時不要使用符號(`&`)。這樣做: **正確:**因為`arr`代表數組的地址。 ```cpp p = arr; ``` **不正確:** ```cpp p = &arr; ``` ### 示例:使用指針遍歷數組 ```cpp #include <iostream> using namespace std; int main(){ //Pointer declaration int *p; //Array declaration int arr[]={1, 2, 3, 4, 5, 6}; //Assignment p = arr; for(int i=0; i<6;i++){ ? ? cout<<*p<<endl; //++ moves the pointer to next int position ? ? p++; ? ?} return 0; } ``` **輸出:** ```cpp 1 2 3 4 5 6 ``` ## 如何遞增指針地址和指針的值? 當我們通過指針訪問變量的值時,有時我們只需要增加或減少變量的值,或者我們可能需要將指針移動到下一個`int`位置(就像我們在使用數組時一樣)。 [`++`運算符](https://beginnersbook.com/2017/08/cpp-operators/)用于此目的。我們在上面看到的`++`運算符的一個示例,我們通過使用`++`運算符遞增指針值來遍歷數組。讓我們看幾個案例。 ```cpp // Pointer moves to the next int position (as if it was an array) p++; // Pointer moves to the next int position (as if it was an array) ++p; /* All the following three cases are same they increment the value * of variable that the pointer p points. */ ++*p; ++(*p); ++*(p); ```
                  <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>

                              哎呀哎呀视频在线观看