码迷,mamicode.com
首页 > 其他好文 > 详细

typedef与define区别

时间:2014-10-02 13:24:12      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   ar   sp   div   art   

#define 是预处理指令,简单的进行文本替换。

typedef 是编译时处理,作用是给已经存在的类型取一个别名。

由于以上根本原因,导致一下几种区别:

1.#define不会做正确性检查,typedef会。

2.#define可以使用其他说明符对宏名进行扩展,typedef不可以。

3.可以使用typedef可以使用sizeof,#define不可以。

4.typedef是一个语句,所以必须以;结尾,#define没有

5.对指针的操作不同

Typedef int * pint;  
#define PINT int *  
Const pint p;//p不可更改,p指向的内容可以更改,相当于 int * const p;  
Const PINT p;//p可以更改,p指向的内容不能更改,相当于 const int *p;或 int const *p;  
pint s1, s2; //s1和s2都是int型指针  
PINT s3, s4; //相当于int * s3,s4;只有一个是指针。 

 

相关资料:http://developer.51cto.com/art/201104/256060.htm

 

typedef与define区别

标签:style   blog   http   color   使用   ar   sp   div   art   

原文地址:http://www.cnblogs.com/huangcongcong/p/4003916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!