typedef
关键字typedef是为类型起一个别名
例: typedef int INT; //代码中INT和int的作用是一样的
#define
宏定义#define起替换作用,即是简单替换
例: #define INT int; //将代码中int简单替换为INT
二者区别可通过以下例子理解:
typedef int* INTPTR1;
#define INTPTR2 int*;
INTPTR1 a,b; //a,b均为 int* 类型的指针
INTPTR2 c,d; //a为 int* 类型的指针,b为整型
需要注意的是,如果类型的别名是指代了复合类型,那么在使用时会有意想不到的后果,比如:
typedef int * pstint;
num = 10;
const pstint number = #
这里 pstint是 int* 的别名,所以number本身是个常量指针,而不是指向常量的指针,即
const pstint number = &num 可以等效为 int * const number = &num, 而非简单替换后的
const int * number = &num