在我们日常使用数据类型时,有时对于一些特定意义的数据类型,有时起别名会让我们对变量有更有利于我们开发
首先我们先看一下define 和typedef给数据类型起别名的区别 #define uint unsigned int //注意没有;
typedef unsinged int auint;
uint a,b;//实际表示unsigned int a;int b 。为什么是这个结果呢分析原因 由于define是单个文本替换
typedef a,b; //表示 unsigned int a, unsigned int b;
此时展开谈一下typedef给数据类型起别名的用法,typedef不仅可以给原有的数据类型起别名还可以给自己定义的数据类型起别名
typedef int aint;//上面谈过这里不细叙述
给结构体起别名
1.
struct person {
int age;
char sex;
};
typedef struct person aperson;
2.
typedef struct person
{
int age;
char sex;
}aperson;//注意这里是别名而不是结构体变量
2.也可以省略成如下
typedef struct //此种比较常用
{
int age;
char sex;
}aperson;
未起别名前定义变量 struct person xiao;
起别名后定义变量 aperson person;//aperson 代表 struct person
给枚举类型起别名
1.
enum Gender
{
female, //注意这里是逗号
male,
};
typedef enum Gender sex;
2.
typedef enum Gender
{
female,
male,
}sex;
3.
typedef enum
{
female,
male,
}sex;
最后
以上就是糊涂蜡烛最近收集整理的关于c语言给数据类型起别名的全部内容,更多相关c语言给数据类型起别名内容请搜索靠谱客的其他文章。
发表评论 取消回复