解引用运算符
此条目或其章节极大或完全地依赖于某个单一的来源。 (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).