<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/2014/01/c-pointers/](https://beginnersbook.com/2014/01/c-pointers/) **指針**是存儲另一個變量的地址的變量。與保存某種類型值的其他變量不同,指針保存變量的地址。例如,整數變量保存(或者可以說是存儲)整數值,但整數指針保存整數變量的地址。在本指南中,我們將在示例的幫助下討論 [C 編程](https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/)中的指針。 在我們討論 C 中的**指針之前,讓我們舉一個簡單的例子來理解變量地址的含義。** #### 一個簡單的例子,了解如何在沒有指針的情況下訪問變量的地址? 在這個程序中,我們有一個`int`類型的變量。`num`的值是 10,這個值必須存儲在內存中的某個地方,對吧?為保存該變量值的變量分配一個內存空間,該內存空間有一個地址。例如,我們住在一所房子里,我們的房子有一個地址,幫助其他人找到我們的房子。同樣,變量的值存儲在內存地址中,這有助于 C 程序在需要時找到該值。 因此,假設分配給變量`num`的地址是`0x7fff5694dc58`,這意味著我們應該將賦給`num`的任何值存儲在以下位置:`0x7fff5694dc58`。見下圖。 ```c #include <stdio.h> int main() { int num = 10; printf("Value of variable num is: %d", num); /* To print the address of a variable we use %p * format specifier and ampersand (&) sign just * before the variable name like &num. */ printf("\nAddress of variable num is: %p", &num); return 0; } ``` **輸出:** ```c Value of variable num is: 10 Address of variable num is: 0x7fff5694dc58 ``` ![C Pointers](https://img.kancloud.cn/22/4b/224ba9351b4a12564340e045bc1e3c94_500x160.jpg) ## C 中指針的一個簡單例子 該程序顯示如何聲明和使用指針。我們可以使用指針做其他一些事情,我們在本指南后面討論過它們。現在,我們只需要知道如何將指針鏈接到變量的地址。 > **需要注意的重點是**:指針的數據類型和變量必須匹配,`int`指針可以保存`int`變量的地址,類似地用`float`數據類型聲明的指針可以保存`float`變量的地址。在下面的示例中,指針和變量都是`int`類型。 ```c #include <stdio.h> int main() { //Variable declaration int num = 10; //Pointer declaration int *p; //Assigning address of num to the pointer p p = # printf("Address of variable num is: %p", p); return 0; } ``` 輸出: ```c Address of variable num is: 0x7fff5694dc58 ``` ## C 指針 - 與指針一起使用的運算符 讓我們討論一下用于 C 中的指針的運算符。 ### `&`地址運算符 我們在第一個例子中已經看到,我們可以使用`&`符號顯示變量的地址。我用`&num`來訪問變量`num`的地址。**`&`運算符**也稱為**地址運算符**。 ```c printf("Address of var is: %p", &num); ``` **注意事項:**`%p`是一種格式說明符,用于以十六進制格式顯示地址。 既然您知道如何獲取變量的地址,但**如何將該地址存儲在其他變量中?** 這就是指針有用的地方。如本指南開頭所述,C 編程中的指針用于保存另一個變量的地址。 **指針就像另一個變量,主要區別在于它存儲另一個變量的地址而不是值。** ## “地址值”(`*`)運算符 `*`運算符也稱為**地址值運算符**。 **如何聲明指針?** ```c int *p1 /*Pointer to an integer variable*/ double *p2 /*Pointer to a variable of data type double*/ char *p3 /*Pointer to a character variable*/ float *p4 /*pointer to a float variable*/ ``` 以上是指針聲明的幾個例子。 **如果你需要一個指針來存儲整數變量的地址,那么指針的數據類型應該是 `int`** 。同樣的情況與其他數據類型有關。 通過使用`*`運算符,我們可以通過指針訪問變量的值。 例如: ```c double a = 10; double *p; p = &a; ``` `*p`會給我們變量`a`的值。以下語句將顯示 10 作為輸出。 ```c printf("%d", *p); ``` 同樣,如果我們像這樣為`*`指針賦值: ```c *p = 200; ``` 它會改變變量`a`的值。上面的語句會將`a`的值從 10 更改為 200。 ### 使用`&`和`*`的指針示例 ```c #include <stdio.h> int main() { /* Pointer of integer type, this can hold the * address of a integer type variable. */ int *p; int var = 10; /* Assigning the address of variable var to the pointer * p. The p can hold the address of var because var is * an integer type variable. */ p= &var; printf("Value of variable var is: %d", var); printf("\nValue of variable var is: %d", *p); printf("\nAddress of variable var is: %p", &var); printf("\nAddress of variable var is: %p", p); printf("\nAddress of pointer p is: %p", &p); return 0; } ``` 輸出: ```c Value of variable var is: 10 Value of variable var is: 10 Address of variable var is: 0x7fff5ed98c4c Address of variable var is: 0x7fff5ed98c4c Address of pointer p is: 0x7fff5ed98c50 ``` ![pointer_memory_representation](https://img.kancloud.cn/a8/d6/a8d6ae104f7ef1e051eeb56e54f64bbe_500x300.jpg) 讓我們舉更多的例子來更好地理解它,讓我們說我們有一個`char`變量`ch`和一個指向它的指針`ptr`。 ```c char ch='a'; char *ptr; ``` **讀取`ch`** 的值 ```c printf("Value of ch: %c", ch); or printf("Value of ch: %c", *ptr); ``` **改變`ch`** 的值 ```c ch = 'b'; or *ptr = 'b'; ``` 上面的代碼會將值`'a'`替換為`'b'`。 **您能猜出以下 C 程序的輸出嗎?** ```c #include <stdio.h> int main() { int var =10; int *p; p= &var; printf ( "Address of var is: %p", &var); printf ( "\nAddress of var is: %p", p); printf ( "\nValue of var is: %d", var); printf ( "\nValue of var is: %d", *p); printf ( "\nValue of var is: %d", *( &var)); /* Note I have used %p for p's value as it represents an address*/ printf( "\nValue of pointer p is: %p", p); printf ( "\nAddress of pointer p is: %p", &p); return 0; } ``` **輸出:** ```c Address of var is: 0x7fff5d027c58 Address of var is: 0x7fff5d027c58 Value of var is: 10 Value of var is: 10 Value of var is: 10 Value of pointer p is: 0x7fff5d027c58 Address of pointer p is: 0x7fff5d027c50 ``` #### 關于指針的更多主題 1) [**指向指針的指針**](https://beginnersbook.com/2014/01/c-pointer-to-pointer/) - 一個指針可以指向另一個指針(這意味著它可以存儲另一個指針的地址),這樣的指針稱為雙重指針或者指針的指針。 2) [**將指針傳遞給函數**](https://beginnersbook.com/2014/01/c-passing-pointers-to-functions/) - 指針也可以作為參數傳遞給函數,使用此功能可以按引用調用函數,并且可以將數組傳遞給函數。 3) [**函數指針**](https://beginnersbook.com/2014/01/c-function-pointers/) - 函數指針就像另一個指針,它用于存儲函數的地址。函數指針也可用于調用 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>

                              哎呀哎呀视频在线观看