
C 语言通过 typedef 创建一个新的类型名(data type names)(而不是新的类型)[1],下面是一个例子:
1) 用法一:
typedef Name type;
例如:
typedef char* String;
这样,我们就可以使用String来表示char *了,比如:
String str = "hello";
下面是一个复杂的例子
typedef struct tnode
*Treeptr;
typedef struct tnode
{ /* the tree node:
*/
char
*word;
/* points to the text */
int
count;
/* number of occurrences */
struct tnode
*left; /* left
child */
struct tnode
*right; /* right child
*/
}
Treenode;
typedef int (*PFI)(char *, char
*);
这里表示了创建了一个类型名PFI,表示指向一个函数,这个函数返回类型是int,参数为char *, char *。我们可以这样来使用:
PFI a,b; // a, b 为函数指针
[1] 《The C Programming Language》:
C provides a facility called typedef for creating new data type names.
It must be emphasized that a typedef does not create a new type in any sense; it merely adds a new name for some existing type.
Powered by Haiwit