间接寻址和 Address-of 运算符Indirection and Address-of Operators

一元间接寻址运算符 (*) 通过指针间接访问一个值。The unary indirection operator (*) accesses a value indirectly, through a pointer. 操作数必须是指针类型。The operand must be a pointer type. 操作的结果是操作数所寻址的值;即其操作数指向的地址处的值。The result of the operation is the value addressed by the operand; that is, the value at the address to which its operand points. 结果的类型是操作数寻址的类型。The type of the result is the type that the operand addresses.

如果操作数的类型为指向类型的指针,则间接寻址运算符的结果为类型。The result of the indirection operator is type if the operand is of type pointer to type. 如果操作数指向函数,则结果是函数指示符。If the operand points to a function, the result is a function designator. 如果指向对象,则结果为指定对象的左值。If it points to an object, the result is an lvalue that designates the object.

如果指针值无效,则间接寻址运算符的结果不确定。If the pointer value is not valid, the result of the indirection operator is undefined. 以下是使指针值无效的一些最常见条件:These are some of the most common conditions that invalidate a pointer value:

  • 该指针为 null 指针。The pointer is a null pointer.

  • 在引用过程中,该指针在对象生存期结束后(例如,对象已超出范围或已解除分配)指定其地址。The pointer specifies the address of an object after the end of its lifetime (such as an object that's gone out of scope or that's been deallocated) at the time of the reference.

  • 该指针指定未针对所指向的对象类型正确对齐的地址。The pointer specifies an address that is inappropriately aligned for the type of the object pointed to.

  • 该指针指定执行程序未使用的地址。The pointer specifies an address not used by the executing program.

一元 address-of 运算符 (&) 给出其操作数的地址。The unary address-of operator (&) gives the address of its operand. 操作数必须是用于指定对象的左值,该对象不得声明为“register”,也不得为位域、一元 * 运算符或数组取消引用 ([]) 运算符的结果或函数指示符。The operand must be either an lvalue that designates an object that is not declared register and is not a bit-field, or the result of a unary * operator or an array dereference ([]) operator, or a function designator. 操作数类型为类型时,结果的类型为指向类型的指针。The result is of type pointer to type for an operand of type type.

如果操作数是一元 * 运算符的结果,则不对两个运算符进行运算,并且结果像是同时省略了这两个运算符。If the operand is the result of a unary * operator, neither operator is evaluated and the result is as if both were omitted. 结果不为左值,运算符约束仍适用。The result is not an lvalue, and the constraints on the operators still apply. 如果操作数是 [] 运算符的结果,则不会对 & 运算符进行运算,也不会对 [] 运算符暗含的一元 * 进行运算。If the operand is the result of a [] operator, neither the & operator nor the unary * implied by the [] operator is evaluated. 其结果与删除 & 运算符并将 [] 运算符更改为 + 运算符的效果相同。The result has the same effect as removing the & operator and changing the [] operator to a + operator. 否则,结果为指向对象或操作数指定的函数的指针。Otherwise, the result is a pointer to the object or function designated by the operand.

示例Examples

下面的示例使用这些常用声明:The following examples use these common declarations:

int *pa, x;
int a[20];
double d;

此语句使用 address-of 运算符 (&) 来获取数组 a 的第六个元素的地址。This statement uses the address-of operator (&) to take the address of the sixth element of the array a. 结果存储在指针变量 pa 中:The result is stored in the pointer variable pa:

pa = &a[5];

间接寻址运算符 (*) 在此示例中用于访问存储在 pa 中的地址的 int 值。The indirection operator (*) is used in this example to access the int value at the address stored in pa. 将此值分配给整数变量 xThe value is assigned to the integer variable x:

x = *pa;

此示例表明对 x 的地址应用间接运算符的结果与 x 相同:This example demonstrates that the result of applying the indirection operator to the address of x is the same as x:

assert( x == *&x );

此示例演示用于声明指向函数的指针的等效方法:This example shows equivalent ways of declaring a pointer to a function:

int roundup( void );     /* Function declaration */

int  *proundup  = roundup;
int  *pround  = &roundup;
assert( pround == proundup );

一旦声明函数 roundup,将声明并初始化指向 roundup 的两个指针。Once the function roundup is declared, two pointers to roundup are declared and initialized. 第一个指针为 proundup,它仅通过函数名称进行初始化;第二个指针为 pround,它在初始化中使用 address-of 运算符。The first pointer, proundup, is initialized using only the name of the function, while the second, pround, uses the address-of operator in the initialization. 初始化是等效的。The initializations are equivalent.

请参阅See also

间接寻址运算符:*Indirection Operator: *
Address-of 运算符:&Address-of Operator: &