typedef 是在计算机编程语言中用来为复杂的声明定义简单的别名,它与宏定义有些差异。它本身是一种存储类的关键字,与auto、extern、mutable、static、register等关键字不能出现在同一个表达式中。
1.typedef 类型定义
(1) typedef unsigned long long int uint64;
(2) typedef int Arr[10];
(3) typedef int (*Pfun)(int,int);
(4) typedef int* Pint;
#define PINT int* //注意区分宏与typedef
例:
int Max(int a,int b)
{
return a>b ? a : b;
}
int main()
{
int a = 10;
Pint c,d;
PINT e,f;
c = &a;
d = &a;
e = &a;
//f = &a;//error
uint64 c = 300;
uint64 d = 400;
Arr i; //int
printf("%d\n",sizeof(i));
Pfun fun;
fun = Max;
return 0;
}
2.结构体的应用
输出结构体数组的内容
typedef struct Student
{
char name[20];
int age;
}Student; //等同下面的写法
/*struct Student
{
char name[20];
int age;
};
typedef struct Student Student;
*/
//输出结构体数组的内容
void Show(Student *arr,int len)
{
for(int i=0;i<len;i++)
{
printf("%s,%d\n",arr[i].name,arr[i].age);
}
}
int main()
{
Student arr[] = {{"liubei",19},{"caocao",20},"sunquan",18};
arr[0].age = 23;//修改"liubei"的年龄为23
Show(arr,sizeof(arr)/sizeof(arr[0]));
return 0;
}