张赐荣——一位视障程序员。
赐荣小站: www.prc.cx

張賜榮

张赐荣的技术博客

博客园 首页 新随笔 联系 订阅 管理

定义基本类型的别名

#include <stdio.h>
typedef int Integer;
int main(void)
{
Integer a = 5,b = 10;
printf("%d\n",a+b);
return 0;
}

/*
typedef不是必须声明在函数外,也可以声明在一个函数中,只不过只有该函数才可以使用声明的类型别名;
typedef修饰的语句看起来像是定义了一个变量,其实变量的位置只是类型的别名,如果把类型说明符后面的当作变量,编译器会提示变量未定义。
*/


定义复合类型的别名

#include <stdio.h>
typedef struct student
{
char *name;
int age;
}stu,*s;
int main(void)
{
stu a;
s b;
a.name = "sam";
a.age = 15;
b = &a;
printf("%s\n%d\n",b->name,b->age);
return 0;
}

/*
可以看出,在使用student的别名定义结构体变量与结构体指针变量时,并没有使用struct关键字,所以用typedef修饰的结构体,在定义变量时可以不使用struct关键字;
typedef在给基本数据类型取别名时,类型说明符后面的是类型的别名,不是变量,那么同样,在student结构体的最后是student类型的别名和一个指向student的指针类型的别名,不是结构体变量和结构体指针变量。
*/

posted on 2022-03-21 16:11  张赐荣  阅读(220)  评论(0编辑  收藏  举报

感谢访问张赐荣的技术分享博客!
博客地址:https://cnblogs.com/netlog/
知乎主页:https://www.zhihu.com/people/tzujung-chang
个人网站:https://prc.cx/