Typedef 声明Typedef Declarations

typedef 声明是具有作为存储类的 typedef 的声明。A typedef declaration is a declaration with typedef as the storage class. 声明符将成为新类型。The declarator becomes a new type. 您可以使用 typedef 声明为已由 C 定义的类型或您已声明的类型构造更短和更有意义的名称。You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared. 利用 Typedef 名称,您可以封装可能会发生更改的实现详细信息。Typedef names allow you to encapsulate implementation details that may change.

typedef 声明的解释方式与变量或函数声明的解释方式相同,只不过标识符没有假定由声明指定的类型,而是成为了该类型的同义词。A typedef declaration is interpreted in the same way as a variable or function declaration, but the identifier, instead of assuming the type specified by the declaration, becomes a synonym for the type.

语法Syntax

declarationdeclaration:
declaration-specifiers init-declarator-list opt;declaration-specifiers init-declarator-list opt;

declaration-specifiersdeclaration-specifiers:
storage-class-specifier declaration-specifiers optstorage-class-specifier declaration-specifiers opt

type-specifier declaration-specifiers opttype-specifier declaration-specifiers opt

type-qualifier declaration-specifiers opttype-qualifier declaration-specifiers opt

storage-class-specifierstorage-class-specifier:
typedef

type-specifiertype-specifier:
voidvoid

charchar

shortshort

intint

longlong

floatfloat

doubledouble

signedsigned

unsignedunsigned

struct-or-union-specifierstruct-or-union-specifier

enum-specifierenum-specifier

typedef-nametypedef-name

typedef-nametypedef-name:
identifieridentifier

请注意,typedef 声明不会创建类型,Note that a typedef declaration does not create types. 而是创建现有类型的同义词或可通过其他方式指定的类型的名称。It creates synonyms for existing types, or names for types that could be specified in other ways. 当使用 typedef 名称作为类型说明符时,可以将其与特定的类型说明符组合,但不可以将其与其他类型说明符组合。When a typedef name is used as a type specifier, it can be combined with certain type specifiers, but not others. 可接受的修饰符包括 const 和 volatileAcceptable modifiers include const and volatile.

Typedef 名称与普通标识符共享命名空间(有关详细信息,请参阅命名空间)。Typedef names share the name space with ordinary identifiers (see Name Spaces for more information). 因此,程序可以有一个 typedef 名称和一个具有相同名称的本地范围标识符。Therefore, a program can have a typedef name and a local-scope identifier by the same name. 例如:For example:

typedef char FlagType;  

int main()  
{  
}  

int myproc( int )  
{  
    int FlagType;  
}  

当通过与 typedef 相同的名称声明本地范围标识符时,或者在同一范围内或内部范围内声明结构或联合的成员时,必须指定类型说明符。When declaring a local-scope identifier by the same name as a typedef, or when declaring a member of a structure or union in the same scope or in an inner scope, the type specifier must be specified. 以下示例演示了此约束:This example illustrates this constraint:

typedef char FlagType;  
const FlagType x;  

若要对标识符、结构成员或联合成员重用 FlagType 名称,则必须提供类型:To reuse the FlagType name for an identifier, a structure member, or a union member, the type must be provided:

const int FlagType;  /* Type specifier required */  

仅仅编写以下语句是不够的It is not sufficient to say

const FlagType;      /* Incomplete specification */  

由于 FlagType 被当做该类型的一部分,因此没有要重新声明的标识符。because the FlagType is taken to be part of the type, not an identifier that is being redeclared. 此声明被视为非法声明,例如This declaration is taken to be an illegal declaration like

int;  /* Illegal declaration */  

可以使用 typedef 声明任何类型,包括指针、函数和数组类型。You can declare any type with typedef, including pointer, function, and array types. 只要定义具有与声明相同的可见性,那么在定义结构或联合类型之前,您就可以为指向结构或联合类型的指针声明 typedef 名称。You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration.

Typedef 名称可用于提高代码可读性。Typedef names can be used to improve code readability. signal 的所有以下三个声明指定了完全相同的类型,第一个声明没有使用任何 typedef 名称。All three of the following declarations of signal specify exactly the same type, the first without making use of any typedef names.

typedef void fv( int ), (*pfv)( int );  /* typedef declarations */  

void ( *signal( int, void (*) (int)) ) ( int );  
fv *signal( int, fv * );   /* Uses typedef type */  
pfv signal( int, pfv );    /* Uses typedef type */  

示例Examples

以下示例演示了 typedef 声明:The following examples illustrate typedef declarations:

typedef int WHOLE; /* Declares WHOLE to be a synonym for int */  

请注意,WHOLE 现在可用于变量声明,如 WHOLE i;const WHOLE i;Note that WHOLE could now be used in a variable declaration such as WHOLE i; or const WHOLE i;. 但是,声明 long WHOLE i; 是非法的。However, the declaration long WHOLE i; would be illegal.

typedef struct club   
{  
    char name[30];  
    int size, year;  
} GROUP;  

此语句将 GROUP 声明为具有三个成员的结构类型。This statement declares GROUP as a structure type with three members. 由于也指定了结构标记 club,因此 typedef 名称 (GROUP) 或结构标记可用于声明。Since a structure tag, club, is also specified, either the typedef name (GROUP) or the structure tag can be used in declarations. 您必须使用带标记的 struct 关键字,并且不能使用带 typedef 名称的 struct 关键字。You must use the struct keyword with the tag, and you cannot use the struct keyword with the typedef name.

typedef GROUP *PG; /* Uses the previous typedef name   
                      to declare a pointer            */  

类型 PG 被声明为指向 GROUP 类型的指针,而 类型又被定义为结构类型。The type PG is declared as a pointer to the GROUP type, which in turn is defined as a structure type.

typedef void DRAWF( int, int );  

此示例为不返回值并采用两个 int 参数的函数提供了类型 DRAWFThis example provides the type DRAWF for a function returning no value and taking two int arguments. 例如,这意味着声明This means, for example, that the declaration

DRAWF box;   

等效于声明is equivalent to the declaration

void box( int, int );  

另请参阅See Also