typedef和宏的关键性区别主要体现在两个方面:
1. 可以用其它类型符对宏定义的类型符进行扩展,而typedef却不行
#define peach int
unsigned peach i; /* OK*/
typedef peach int;
unsigned peach i; /*Error!*/
2. 在连续几个变量的声明中,typedef能保证这个变量的类型一致,而define则不可以
#define int_ptr int *
int_ptr chalk, cheese;
经过宏扩展,上面的表达示变为:
int * chalk, cheese;
可以看出chalk变定义成了int指针,而cheese则变成了一个int变量。typedef int * int_ptr;
int_ptr chalk, cheese; /* OK */