1,基本类型取别名
例如: typedef int I;
2,数组类型取别名
例如: typedef int ARR[5];
3,指针类型取别名
例如: typedef int* IP;
例1:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
/*
* 类型取别名方法:
* 1,先写出该类型定义变量的语句格式
* 2,在语句的前面加上关键字:typedef,此时,变量名就是类型的别名
*
* */
typedef int a; //a为int别名
typedef int b,c; //b和c都是int的别名
typedef int d,*p; //d为int类型别名,p为int*类型的别名
int main(void)
{
a a1 = 100;
b b1 = 200;
c c1 = 300;
d d1 = 400;
p p1;
p1 = &a1;
printf("*p1 = %d\n",*p1);
p1 = &b1;
printf("*p1 = %d\n",*p1);
p1 = &c1;
printf("*p1 = %d\n",*p1);
p1 = &d1;
printf("*p1 = %d\n",*p1);
return 0;
}
例2:
typedef struct node{
int data;
struct node *next;
}Node,*PNode; //Node为struct node的别名,PNode为struct node*的别名
int main(void)
{
Node s1 = {100,NULL};
Node s2 = {200,&s1};
Node s3 = {300,&s2};
PNode p;
for(p = &s3; p != NULL; p = p->next)
printf("p->data = %d\n",p->data);
return 0;
}