解引用運算符
此條目或其章節極大或完全地依賴於某個單一的來源。 (2022年3月5日) |
解引用運算符(dereference operator)或稱間址運算符(indirection operator)。[1]C語言程式語言家族中常表示為"*
" (一個星號),為單元運算符,作用於1個指針變量,返回該指針地址的等效左值
。這被稱為指針的解引用。例如:
int x;
int *p; // * is used in the declaration:
// p is a pointer to an integer, since (after dereferencing),
// *p is an integer
x = 0;
// now x == 0
p = &x; // & takes the address of x
// now *p == 0, since p == &x and therefore *p == x
*p = 1; // equivalent to x = 1, since p == &x
// now *p == 1 and x == 1
在C語言中,&
是解引用運算符*
的逆運算。因此*&s
等價於s
。例如:
p = &s; // the address of s has been assigned to p; p == &s;
// *p is equivalent to s
結構s
的成員a
的值可表示為s.a
。若指針p
指向s
(即p == &s
), s.a
等價於(*p).a
,也可用結構解引用運算符作為語法糖縮寫表示為p->a
:
p = &s; // the address of s has been assigned to p; p == &s;
// s.a is equivalent to (*p).a
// s.a is equivalent to p->a
// (*p).a is equivalent to p->a
參見
編輯參考文獻
編輯- ^ Pointer related operators (C# reference), source:MSDN. [2022-03-04]. (原始內容存檔於2022-07-09).