1.可以用其他类型说明符对宏类型进行扩展,但对typedef所定义的类型名却不能这样做。如下所示:

  

#define peach int
unsigned peach i;
/*没问题*/
typedef
int banana;
unsigned banana i;
/*错误,非法!*/

2.在连续几个变量的声明中,用typedef定义的类型能够保证声明中的所有变量均为同一种类型,而用#define定义的类型则无法保证,如下所示:

#define int_ptr int *;
int_ptr chalk,cheese;

经过宏扩展,第二行变为:

int * chalk,cheese;

这使得chalk和cheese成为不同的类型。

相反,下面的代码:

typedef char * char_ptr;
char_ptr Bentley,Rolls_Royce;
则能保证Bentley,Rolls_Royce类型相同,都是指向char的指针。

相关文章:

  • 2021-07-28
  • 2022-12-23
  • 2022-01-04
  • 2021-09-17
  • 2021-08-16
  • 2021-09-07
  • 2021-07-09
猜你喜欢
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
  • 2021-05-17
  • 2021-06-01
  • 2022-12-23
相关资源
相似解决方案