解引用运算符

解引用运算符(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

Unix的外壳脚本以及一些工具软件如Makefile中,美元符"$"为解引用运算符,用于把变量名字转译为其内容。

参见 编辑

参考文献 编辑

  1. ^ Pointer related operators (C# reference), source:MSDN. [2022-03-04]. (原始内容存档于2022-07-09).