正在加载...
 
< [转载]HTML特殊...
优美的C语言 ---... >
优美的C语言 --- C中的typedef 
  主题:[编程] | 标签:C/C++ | 浏览数(1778) | 评论数(0) | 2007-04-21

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;

  

2) 用法二

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.

http://www.i170.com/Article/64000/trackback

评论:

Powered by Haiwit