<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://www.programiz.com/c-programming/c-pointers](https://www.programiz.com/c-programming/c-pointers) #### 在本教程中,您將學習指針。 什么是指針,如何使用它們以及在示例的幫助下使用它們時可能遇到的常見錯誤。 指針是 C 和 C++ 編程的強大功能。 在學習指針之前,讓我們學習一下 C 編程中的地址。 * * * ## C 地址 如果程序中有變量`var`,則`&var`將在內存中提供其地址。 使用`scanf()`函數時,我們已多次使用地址。 ```c scanf("%d", &var); ``` 在此,用戶輸入的值存儲在`var`變量的地址中。 讓我們舉一個可行的例子。 ```c #include <stdio.h> int main() { int var = 5; printf("var: %d\n", var); // Notice the use of & before var printf("address of var: %p", &var); return 0; } ``` **輸出** ```c var: 5 address of var: 2686778 ``` **注意**:運行上面的代碼時,您可能會獲得其他地址。 * * * ## C 指針 指針(指針變量)是用于存儲地址而非值的特殊變量。 ### 指針語法 這是我們如何聲明指針的方法。 ```c int* p; ``` 在這里,我們聲明了`int`類型的指針`p`。 您也可以通過以下方式聲明指針。 ```c int *p1; int * p2; ``` * * * 讓我們再舉一個聲明指針的例子。 ```c int* p1, p2; ``` 在這里,我們聲明了一個指針`p1`和一個正常變量`p2`。 * * * ### 給指針分配地址 讓我們舉個例子。 ```c int* pc, c; c = 5; pc = &c; ``` 在此,將`c`變量分配為 5。 并且,將`c`的地址分配給`pc`指針。 * * * ### 獲取指針所指的值 要獲取指針所指向的值,我們使用`*`運算符。 例如: ```c int* pc, c; c = 5; pc = &c; printf("%d", *pc); // Output: 5 ``` 此處,`c`的地址分配給`pc`指針。 為了獲得存儲在該地址中的值,我們使用了`* pc`。 **注意**:在上面的示例中,`pc`是指針,而不是`*pc`。 您不能也不應做類似`*pc = &c`的操作; 順便說一下,`*`被稱為解除引用運算符(使用指針時)。 它對指針進行操作并給出存儲在該指針中的值。 * * * ### 改變指針指向的值 讓我們舉個例子。 ```c int* pc, c; c = 5; pc = &c; c = 1; printf("%d", c); // Output: 1 printf("%d", *pc); // Ouptut: 1 ``` 我們已經將`c`的地址分配給`pc`指針。 然后,我們將`c`的值更改為 1。由于`pc`和`c`的地址相同,所以`*pc`給出 1。 讓我們再舉一個例子。 ```c int* pc, c; c = 5; pc = &c; *pc = 1; printf("%d", *pc); // Ouptut: 1 printf("%d", c); // Output: 1 ``` We have assigned the address of `c` to the `pc` pointer. 然后,我們使用`*pc = 1;`將`*pc`更改為 1。 由于`pc`和`c`的地址相同,因此`c`等于 1。 讓我們再舉一個例子。 ```c int* pc, c, d; c = 5; d = -15; pc = &c; printf("%d", *pc); // Output: 5 pc = &d; printf("%d", *pc); // Ouptut: -15 ``` 最初,使用`pc = &c;`將`c`的地址分配給`pc`指針。 由于`c`為 5,因此`*pc`給我們 5。 然后,使用`pc = &d;`將`d`的地址分配給`pc`指針。 由于`d`為 -15,因此`*pc`給我們 -15。 * * * ### 示例:指針的工作原理 讓我們舉一個可行的例子。 ```c #include <stdio.h> int main() { int* pc, c; c = 22; printf("Address of c: %p\n", &c); printf("Value of c: %d\n\n", c); // 22 pc = &c; printf("Address of pointer pc: %p\n", pc); printf("Content of pointer pc: %d\n\n", *pc); // 22 c = 11; printf("Address of pointer pc: %p\n", pc); printf("Content of pointer pc: %d\n\n", *pc); // 11 *pc = 2; printf("Address of c: %p\n", &c); printf("Value of c: %d\n\n", c); // 2 return 0; } ``` **輸出** ```c Address of c: 2686784 Value of c: 22 Address of pointer pc: 2686784 Content of pointer pc: 22 Address of pointer pc: 2686784 Content of pointer pc: 11 Address of c: 2686784 Value of c: 2 ``` * * * **程序的說明** 1. `int* pc, c;` ![A pointer variable and a normal variable is created.](https://img.kancloud.cn/7a/4c/7a4cee41d82f70f092525c059c6cd090_120x103.png) 此處,創建了類型均為`int`的指針`pc`和常規變量`c`。 由于`pc`和`c`最初并未初始化,因此指針`pc`指向無地址或隨機地址。 并且,變量`c`具有地址,但包含隨機垃圾值。 2. `c = 22;` ![22 is assigned to variable c.](https://img.kancloud.cn/4d/a9/4da9bbef94dd55ae8130f74378ccbc38_120x97.png) 這將 22 分配給變量`c`。 即,將 22 存儲在變量`c`的存儲單元中。 3. `pc = &c;` ![Address of variable c is assigned to pointer pc.](https://img.kancloud.cn/7c/2d/7c2de68a21f7e15f48d78d0adc4fe329_120x97.png) 這將變量`c`的地址分配給指針`pc`。 4. `c = 11;` ![11 is assigned to variable c.](https://img.kancloud.cn/9d/08/9d089117ab9d05ddd18218a5ef942e85_121x102.png) 這將 11 分配給變量`c`。 5. `*pc = 2;` ![5 is assigned to pointer variable's address.](https://img.kancloud.cn/76/70/767065d828b5834287101c4329500eba_119x100.png) 將指針`pc`指向的存儲位置的值更改為 2。 * * * ### 使用指針時的常見錯誤 假設您希望指針`pc`指向`c`的地址。 然后, ```c int c, *pc; // pc is address but c is not pc = c; // Error // &c is address but *pc is not *pc = &c; // Error // both &c and pc are addresses pc = &c; // both c and *pc values *pc = c; ``` 這是指針語法初學者經常會感到困惑的示例。 ```c #include <stdio.h> int main() { int c = 5; int *p = &c; printf("%d", *p); // 5 ? return 0; } ``` **使用`int?*p = &c;`時為什么沒有出現錯誤?** 這是因為 ```c int *p = &c; ``` 相當于 ```c int *p: p = &c; ``` 在這兩種情況下,我們都將創建一個指針`p`(不是`*p`)并為其分配`&c`。 為了避免這種混亂,我們可以使用如下語句: ```c int* p = &c; ``` * * * 現在您知道了什么是指針,您將在下一個教程中學習指針與數組的關系。
                  <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>

                              哎呀哎呀视频在线观看