typedef的作用:1 为现有类型取一个别名,实现代码的跨平台使用
2 简化代码
实例:
A: typedef double D;
D d = 23.4;
B:typedef int A[5];
A str="abcd";
C:结构体
typedef struct{
int age;
}Student;
Student stu={20};
或者:
struct Person{
int age;
};
typedef struct Person Student;
Student stu={35};
D:typedef char *Ptr;
char a='A';
Ptr ch = &a;
E:typedef char (*Str)[5];
char istr[]="abcd";
Str str = &istr; //数组名代表整个一维数组的地址
cout<<(*str)[1];
F:typedef int * Fun(int, int);
int * max(int a, int b){return a>b?&a:&b;}
Fun *fun = &max;
cout<<*((*fun)(5, 6));
G:typedef int (*Fun)(int, int); //指向一个函数
int max(int a, int b){return a>b?a:b;}
Fun fun = max;
cout<<fun(5, 6);
H:方便实现返回函数入口地址
int max(int a, int b){return a>b?a:b;}
typedef int (*Fun)(int, int);
Fun getMethod(Fun fun){
return fun;
}
void main(){
Fun fun = max;
Fun max = getMethod(fun);
printf("%d", max(5, 6));
}