typedef的陷阱

来源:互联网 发布:淘宝众筹回报是什么 编辑:程序博客网 时间:2024/04/30 10:53

typedef      int*     PINT; 

#define     pint   int*

const PINT p1;           // the unit where p1 pointed can be changed

const pint   p2;          // p2 can't  be changed

 

const   PINT  p1    =>   int*  const   p1;

const  pint     p2    =>   const int*    p2;

const修饰词和其他类型修饰是并列的,即它们共同来修饰一个变量对象

因为当用typedef定义了一种新的类型PINT之后,const修饰的对象就是PINT,而PINT本身是指针,于是const PINT的意思就是PINT(某种指针)的值是常量,所以最后p1就被理解为指向intconst指针。

原创粉丝点击