C語言中指標的解引用



C語言中指標的解引用

**解引用運算子** 用於訪問和操作由指標指向的變數中儲存的值。**解引用**或**間接運算子**(***) 充當一元運算子,它需要一個指標變數作為其運算元。

語法

以下是解引用指標的語法:

*pointer_variable;

藉助上述語法(解引用指標),您可以獲取和更新任何由指標指向的變數的值。

如何解引用指標?

要解引用指標,您需要按照以下步驟操作:

  • 建立一個變數並宣告一個指標變數。
  • 透過賦值變數的地址來初始化指標。
  • 現在,您可以解引用指標來獲取或更新變數的值。

示例

在此示例中,我們演示了這三個步驟來解引用指標:

#include <stdio.h>

int main() {
  // Create a variable and pointer variable
  int x = 10;
  int *ptr;

  // Initialize the pointer by assigning
  // the address of the variable
  ptr = &x;

  // Dereference the pointer
  printf("Value of x = %d\n", *ptr);

  return 0;
}

輸出

執行程式碼並檢查其輸出:

Value of x = 10

什麼是解引用?

術語“解引用”是指訪問指標引用的記憶體地址中儲存的值。解引用運算子(也稱為**間接運算子**)獲取目標變數的值。

示例

在上例中,如果我們列印“*b”,您將獲得“a”的值,即 10。類似地,列印“*y”將顯示 10.5。

#include <stdio.h>

int main (){

   int a = 10;
   int *b = &a;
   float x = 10.5;
   float *y = &x;

   printf ("Address of 'a': %d Value of 'a': %d\n", b, *b);
   printf ("Address of 'x': %d Value of 'x': %f\n", y, *y);

   return 0;
}

輸出

執行程式碼並檢查其輸出:

Address of 'a': 6422028 Value of 'a': 10
Address of 'x': 6422024 Value of 'x': 10.500000

透過解引用指標操作值

解引用運算子還有助於間接操作指標引用的變數的值。

示例

在此示例中,我們使用解引用指標更改“a”和“x”的值:

#include <stdio.h>

int main (){

   int a = 10;
   int *b = &a;
   float x = 10.5;
   float *y = &x;

   *b = 100;
   *y = 100.50;

   printf ("Address of 'a': %d Value of 'a': %d\n", b, *b);
   printf ("Address of 'x': %d Value of 'x': %f\n", y, *y);

   return 0;
}

輸出

執行程式碼並檢查其輸出:

Address of 'a': 6422028 Value of 'a': 100
Address of 'x': 6422024 Value of 'x': 100.500000

解引用雙指標

就像您將普通變數的地址儲存在其指標中一樣,您也可以擁有一個儲存另一個指標地址的指標。擁有另一個指標地址的指標稱為雙指標或指向指標的指標。

讓我們宣告一個指向整數型別的指標,並將整數變數的地址儲存在其中。

int a = 10;
int *b = &a;

解引用運算子透過指標獲取值:

printf("a: %d \n Pointer to 'a' is 'b': %d \n Value at 'b': %d", a, b, *b);

整數變數的值、其地址以及解引用指標獲得的值將被列印為:

a: 10 
Pointer to 'a' is 'b': 6422036 
Value at 'b': 10

現在讓我們宣告一個可以儲存“b”地址的指標,“b”本身是指向整數型別的指標,寫成“int *”。讓我們假設編譯器也為其分配地址 3000。因此,“c”是指向指向int的指標,應宣告為“int **”。

int **c = &b;
printf("b: %d \n Pointer to 'b' is 'c': %d \n Value at 'b': %d\n", b, c, *c);

您可以獲得b的值(它是a的地址)、c的值(它是b的地址)以及從c解引用的值(它是a的地址)。

b: 6422036 
Pointer to 'b' is 'c': 6422024 
Value at 'b': 6422036

由於這裡“c”是雙指標,宣告中的第一個星號指向“b”,第二個星號依次指向“a”。我們可以使用雙重引用指標從“c”獲得“a”的值。

printf("Value of 'a' from 'c': %d", **c);

這應該顯示a的值為10。

示例

嘗試執行以下完整程式碼:

#include <stdio.h>

int main (){

   int a = 10;
   int *b = &a;
   printf("a: %d \n Address: %d \n Value at 'a': %d\n\n", a, b, *b);

   int **c = &b;
   printf("b: %d \n Pointer to 'b' is 'c': %d \n Value at 'b': %d\n", b, c, *c);
   printf("Value of 'a' from 'c': %d", **c);

   return 0;
}

輸出

執行此程式碼時,將產生以下輸出:

a: 10 
Address: 6422036 
Value at a: 10

b: 6422036 
Pointer to 'b' is 'c': 6422024 
Value at 'b': 6422036
Value of 'a' from 'c': 10

解引用結構體指標

關鍵字“struct”用於建立一個派生資料型別,該型別由一個或多個不同型別元素組成。像普通變數一樣,您可以宣告一個結構體指標並存儲其地址。

struct book{
   char title[10];
   double price;
   int pages;
};

struct book b1 = {"Learn C", 650.50, 325};
struct book *ptr = &b1;

在C語言中,用箭頭符號 (→) 表示的間接運算子用於獲取結構體指標引用的結構體變數的元素的值。

示例

ptr -> title”返回title元素的值,與“b1.title”返回的值相同。“ptr -> price”等同於“b1.price”等。

#include <stdio.h>

struct book{
   char title[10];
   double price;
   int pages;
};

int main (){

   struct book b1 = {"Learn C", 650.50, 325};
   struct book *ptr = &b1;

   printf("With -> Operator: \n");
   printf("Title: %s \nPrice: %7.2lf \nNumber of Pages: %d\n\n", ptr->title, ptr->price, ptr->pages);

   printf("With . Operator:\n");
   printf("Title: %s \nPrice: %7.2lf \nNumber of Pages: %d\n", b1.title, b1.price, b1.pages);

   return 0;
}

輸出

執行此程式碼時,將產生以下輸出:

With -> Operator: 
Title: Learn C 
Price:  650.50 
Number of Pages: 325

With . Operator:
Title: Learn C 
Price:  650.50 
Number of Pages: 325

解引用巢狀結構體指標

儘管C語言使用箭頭運算子 (→) 來訪問結構體變數的元素,但任何內部結構體的元素都不能用它來訪問。

只有外部結構體的元素才能用 → 運算子訪問。對於後續的內部結構體元素,我們需要使用點(.)運算子。

示例

以下示例顯示如何解引用巢狀結構體指標:

#include <stdio.h>
#include <string.h>

struct employee{
   char name[10];
   float salary;

   struct dob {
      int d, m, y;
   } d1;
};

int main(){

   struct employee e1 = {"Arjun", 45000, {12, 5, 1990}};
   struct employee *ptr = &e1;

   printf("Name: %s\n", ptr->name);
   printf("Salary: %f\n", ptr->salary);
   printf("Date of Birth: %d-%d-%d\n", ptr->d1.d, ptr->d1.m, ptr->d1.y);

   return 0;
}

輸出

執行此程式碼時,將產生以下輸出:

Name: Arjun
Salary: 45000.000000
Date of Birth: 12-5-1990
廣告