指针声明Pointer Declarations

“指针声明”可命名指针变量并指定该变量所指向的对象的类型。A "pointer declaration" names a pointer variable and specifies the type of the object to which the variable points. 声明为指针的变量保留了一个内存地址。A variable declared as a pointer holds a memory address.

语法Syntax

declarator:declarator:
  pointeropt direct-declarator  pointeropt direct-declarator

direct-declaratordirect-declarator:
  identifier  identifier

  ( declarator )  ( declarator )

  direct-declarator [ constant-expressionopt ]  direct-declarator [ constant-expressionopt ]

  direct-declarator ( parameter-type-list )  direct-declarator ( parameter-type-list )

  direct-declarator ( identifier-listopt )  direct-declarator ( identifier-listopt )

pointer:pointer:
  \* type-qualifier-listopt  \* type-qualifier-listopt

  \* type-qualifier-listopt pointer  \* type-qualifier-listopt pointer

type-qualifier-list:type-qualifier-list:
  type-qualifier  type-qualifier

  type-qualifier-list type-qualifier  type-qualifier-list type-qualifier

type-specifier 用于指定对象的类型,可以是任何基本、结构或联合类型。The type-specifier gives the type of the object, which can be any basic, structure, or union type. 指针变量也可以指向函数、数组和其他指针。Pointer variables can also point to functions, arrays, and other pointers. (有关声明和解释更复杂的指针类型的信息,请参阅解释更复杂的声明符。)(For information on declaring and interpreting more complex pointer types, refer to Interpreting More Complex Declarators.)

通过将 type-specifier 设为 void,可以延迟指针引用的类型的规范。By making the type-specifier void, you can delay specification of the type to which the pointer refers. 这种项称为“指向 void 的指针”,作为 void * 写入。Such an item is referred to as a "pointer to void" and is written as void *. 声明为指向 void 的指针的变量可用于指向任何类型的对象。A variable declared as a pointer to void can be used to point to an object of any type. 但是,若要对指针或指针指向的对象执行大多数操作,则必须为每个操作显式指定指针指向的类型。However, to perform most operations on the pointer or on the object to which it points, the type to which it points must be explicitly specified for each operation. char \* 类型和 void \* 类型的变量不用类型强制转换即可赋值兼容。)此类转换可使用类型强制转换完成(有关详细信息,请参阅类型强制转换)。(Variables of type char \* and type void \* are assignment-compatible without a type cast.) Such conversion can be accomplished with a type cast (see Type-Cast Conversions for more information).

type-qualifier 可以是 constvolatile 之一,也可以是这两者。The type-qualifier can be either const or volatile, or both. 它们分别指定了程序本身无法修改的指针 (const),或程序的控制之外的某些进程可以合理修改的指针 (volatile)。These specify, respectively, that the pointer cannot be modified by the program itself (const), or that the pointer can legitimately be modified by some process beyond the control of the program (volatile). (有关 constvolatile 的详细信息,请参阅类型限定符。)(See Type Qualifiers for more information on const and volatile.)

declarator 可为变量命名,并可包含类型修饰符。The declarator names the variable and can include a type modifier. 例如,如果 declarator 表示数组,则将指针的类型修改为指向数组的指针。For example, if declarator represents an array, the type of the pointer is modified to be a pointer to an array.

在定义结构、联合或枚举类型之前,您可以声明指向结构、联合或枚举类型的指针。You can declare a pointer to a structure, union, or enumeration type before you define the structure, union, or enumeration type. 您可使用结构或联合标记声明指针,如下面的示例所示。You declare the pointer by using the structure or union tag as shown in the examples below. 此类声明是允许的,因为编译器不需要知道要为指针变量分配空间的结构或联合的大小。Such declarations are allowed because the compiler does not need to know the size of the structure or union to allocate space for the pointer variable.

示例Examples

以下示例演示了指针声明。The following examples illustrate pointer declarations.

char *message; /* Declares a pointer variable named message */  

message 指针指向具有 char 类型的变量。The message pointer points to a variable with char type.

int *pointers[10];  /* Declares an array of pointers */  

指针数组有 10 个元素;每个元素都是一个指向具有 int 类型的变量的指针。The pointers array has 10 elements; each element is a pointer to a variable with int type.

int (*pointer)[10]; /* Declares a pointer to an array of 10 elements */  

指针变量指向具有 10 个元素的数组。The pointer variable points to an array with 10 elements. 此数组中的每个元素都有 int 类型。Each element in this array has int type.

int const *x;      /* Declares a pointer variable, x,  
                      to a constant value */   

可以将指针 x 修改为指向不同的 int 值,但无法修改该指针指向的值。The pointer x can be modified to point to a different int value, but the value to which it points cannot be modified.

const int some_object = 5 ;  
int other_object = 37;  
int *const y = &fixed_object;  
int volatile *const z = &some_object;  
int *const volatile w = &some_object;  

这些声明中的变量 y 被声明为指向 int 值的常量指针。The variable y in these declarations is declared as a constant pointer to an int value. 可以修改该指针指向的值,但指针本身必须始终指向同一位置:fixed_object 的地址。The value it points to can be modified, but the pointer itself must always point to the same location: the address of fixed_object. 同样,z 是常量指针,但它也被声明为指向值不能由程序修改的 intSimilarly, z is a constant pointer, but it is also declared to point to an int whose value cannot be modified by the program. 附加说明符 volatile 指示,尽管程序无法修改 z 所指向的 const int 的值,但当前与程序一起运行的进程可以合理地修改该值。The additional specifier volatile indicates that although the value of the const int pointed to by z cannot be modified by the program, it could legitimately be modified by a process running concurrently with the program. w 的声明指定,程序无法更改指向的值,并且程序无法修改指针。The declaration of w specifies that the program cannot change the value pointed to and that the program cannot modify the pointer.

struct list *next, *previous; /* Uses the tag for list */  

本示例声明了指向结构类型 list 的两个指针变量 nextpreviousThis example declares two pointer variables, next and previous, that point to the structure type list. 只要 list 类型定义与声明具有相同的可见性,此声明就可以出现在 list 结构类型的定义前面(请参阅下一个示例)。This declaration can appear before the definition of the list structure type (see the next example), as long as the list type definition has the same visibility as the declaration.

struct list   
{  
    char *token;  
    int count;  
    struct list *next;  
} line;  

变量 line 具有名为 list 的结构类型。The variable line has the structure type named list. list 结构类型有三个成员:第一个成员是指向 char 值的指针,第二个成员是 int 值,第三个成员是指向另一个 list 结构的指针。The list structure type has three members: the first member is a pointer to a char value, the second is an int value, and the third is a pointer to another list structure.

struct id   
{  
    unsigned int id_no;  
    struct name *pname;  
} record;  

变量 record 具有结构类型 id。请注意,pname 被声明为指向名为 name 的另一个结构类型的指针。The variable record has the structure type id. Note that pname is declared as a pointer to another structure type named name. 此声明可在定义 name 类型之前出现。This declaration can appear before the name type is defined.

另请参阅See Also

声明符和变量声明Declarators and Variable Declarations