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

间接寻址运算符 (*) 通过指针间接访问一个值。The indirection operator (\*) accesses a value indirectly, through a pointer. 操作数必须是一个指针值。The operand must be a pointer value. 操作的结果是操作数所寻址的值;即其操作数指向的地址处的值。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.

如果操作数指向函数,则结果是函数指示符。If the operand points to a function, the result is a function designator. 如果它指向存储位置,则结果是指定存储位置的左值。If it points to a storage location, the result is an l-value designating the storage location.

如果该指针的值无效,则结果是未定义的。If the pointer value is invalid, the result is undefined. 以下列表包含使指针值无效的一些最常见条件。The following list includes some of the most common conditions that invalidate a pointer value.

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

  • 该指针指定引用时不可见的本地项的地址。The pointer specifies the address of a local item that is not visible 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 address-of operator (&) gives the address of its operand. Address-of 运算符的操作数可以是函数指示符,也可以是指定不是位域且不使用 register 存储类说明符声明的对象的左值。The operand of the address-of operator can be either a function designator or an l-value that designates an object that is not a bit field and is not declared with the register storage-class specifier.

    地址操作的结果是一个指向操作数的指针。The result of the address operation is a pointer to the operand. 该指针寻址的类型是操作数的类型。The type addressed by the pointer is the type of the operand.

    address-of 运算符仅适用于具有基本、结构或在文件范围级别声明的联合类型的变量,或仅适用于下标数组引用。The address-of operator can only be applied to variables with fundamental, structure, or union types that are declared at the file-scope level, or to subscripted array references. 在这些表达式中,可在地址表达式中添加或提取不包括 address-of 运算符的常数表达式。In these expressions, a constant expression that does not include the address-of operator can be added to or subtracted from the address expression.

示例Examples

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

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

此语句使用 address-of 运算符:This statement uses the address-of operator:

pa = &a[5];  

Address-of 运算符 (&) 采用数组 a 的第六个元素的地址。The address-of operator (&) takes the address of the sixth element of the array a. 结果存储在指针变量 pa 中。The result is stored in the pointer variable pa.

x = *pa;  

间接寻址运算符 (*) 在此示例中用于访问存储在 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.

if( x == *&x )  
    printf( "True\n" );  

此示例输出单词 True,表明对 x 的地址应用间接运算符的结果与 x 相同。This example prints the word True, demonstrating that the result of applying the indirection operator to the address of x is the same as x.

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

int  *proundup  = roundup;  
int  *pround  = &roundup;  

一旦声明函数 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: &