说明对讨厌的事说不出讨厌,对喜欢的事也总是偷偷摸摸。
– 太宰治 《人间失格》
typedef 来源于英文单词 “type define”,看着英文的意思是类型定义,其实作用应该是类型重命名。
它的作用是给各种类型取别名。
为什么要使用这个关键字来给类型取别名呢?原来的名字不香吗?
其实使用这个关键字也有一些场景:
-
一些复杂的类型,比如一些复杂的指针定义,使用 typedef 后就可以给复杂的类型定义取一个简单的名字,方便使用。
-
方便工程移植,会给一些基本类型取个固定的别名,方便在不同架构或者不同类型的系统中使用,比如如下定义。
typedef signed char int8_t; typedef unsigned char uint8_t; typedef int int16_t; typedef unsigned int uint16_t; typedef long int32_t; typedef unsigned long uint32_t; typedef long long int64_t; typedef unsigned long long uint64_t;



