9.typedef

typedef
typedef type-declaration synonym;

The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition.
理解:typedef类型声明变成了另外一个名字,并不是名字取代了类型,(言外之意就是,名字变成了新名字,但是新名字还是代表那个类型)
A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types.

Example

// Example of the typedef keyword
typedef unsigned long ulong;

ulong ul;     // Equivalent to "unsigned long ul;"

typedef struct mystructtag
{
   int   i;
   float f;
   char  c;
} mystruct;

mystruct ms;   // Equivalent to "struct mystructtag ms;"

typedef int (*funcptr)();  // funcptr is synonym for "pointer
                           //    to function returning int"

funcptr table[10];   // Equivalent to "int (*table[10])();"

typedef只是定义了某种类型的别名(synonym)而已。

 

posted @ 2020-06-27 19:30  janeyjhon1  阅读(64)  评论(0)    收藏  举报