first, You can extend a macro typename with other type specifiers, but not a typedef 'd typename.
That is,
#define peach int
unsigned peach i; /* works fine */
typedef int banana;
unsigned banana i; /* Bzzzt! illegal */
Second, a typedef 'd name provides the type for every declarator in a declaration.
#define int_ptr int * int_ptr chalk, cheese;
After macro expansion, the second line effectively becomes: int * chalk, cheese; This makes chalk and cheese as different as chutney and chives: chalk is a pointer-to-an-integer, while cheese is an integer.
In contrast, a typedef like this:
typedef char * char_ptr;
char_ptr Bentley, Rolls_Royce;
declares both Bentley and Rolls_Royce to be the same. The name on the front is different, but they are both a pointer to a char.
博客介绍了宏定义和typedef定义类型名的区别。宏定义的类型名可与其他类型说明符扩展,typedef定义的则不行;typedef定义的名称为声明中的每个声明符提供类型,宏定义在展开后可能使声明的变量类型不同,而typedef声明的变量类型相同。
1066

被折叠的 条评论
为什么被折叠?



