与define定义宏不同,typedef这个关键字的本质是做类型重命名。
#include<stdio.h>
typedef unsigned int u_int;
typedef int* int_p;
typedef int a[10];//a是一种数组类型
typedef struct stu {
char name[20];
int age;
}stu_t;
int main()
{
u_int x = 0;
int_p p = NULL;
stu_t s;
a b;
return 0;
}
另外,typedef有自己的作用域:
typedef和宏的区别
宏是预处理指令,本质是在预处理时进行文本替换,而typedef确是由使用者对已有的类型进行重命名。
因此,typedef具有宏不具有的功能:
#include<stdio.h>
typedef int* ptr_t;
#define PTR_T int*
int main()
{
ptr_t p1, p2;
PTR_T p3, p4;
return 0;
}
可以看到p4的类型是int而不是int *,这是因为PTR_T p3, p4;
这句会被替换为int* p3,p4;
,而typedef则不会,ptr_t
是一种重命名的类型,其用法和int,char,float
等类型类似:
typedef类型重命名以后,不能再使用其他修改类型的关键字(比如unsigned)来拼上这个新的类型,其他关键字(比如const,static可以使用):
存储类型关键字不能同时出现
存储类型关键字一共有五个:auto,extern register,static,typedef
,这五个关键字不能同时使用,比如用typedef重命名一个被static修饰的类型:
const修饰被typedef重命名的指针
int_p
是一个重命名的类型,const修饰的其实是p,所以p不能被修改,但是p指向的空间可以被修改(因为这样并不会改变p本身的值),其用法与const int a
和int const a
中a
不能被修改类似:
这和直接使用原来的int *类型是有区别的:
理解了这些,typedef多层套娃的形式也就很好理解了:
typedef int* int_p; // int_p <---> int*
typedef int_p const int_p_c1; // int_p_c <---> int* const
typedef const int_p c_int_p1; // c_int_p <---> int* const
typedef const int* c_int_p2; // c_int_p <---> const int*
typedef int* const int_p_c2; // int_p_c <---> int* const
值得注意的是,即使我们嵌套了两个const,依然可以修改* p的值:
原因上面说过,const修饰的是p,并不是*p
,所以想要*p
不被修改,就只能使用上面这条语句:typedef const int* c_int_p2;