#include<stdio.h>
int getSum(int a, int b);
/*_____________________________typedef的使用______________________________*/
//1.typedef的作用
//int取个一个别名NSInteger
typedef int NSInteger;
//float取个一个别名WXFloat
typedef float WXFloat;
//在别名的基础上再取别名
typedef WXFloat MyFloat;
//2.typedef和指针
typedef char * string;
//3.typedef和结构体
//定义结构体类型struct Mypoint
struct Mypoint {
float x;
float y;
};
//方式一:
typedef struct Mypoint Point;
//方式二:
typedef struct Myp {
float a;
float b;
}Py;
//方式三
typedef struct
{
float x;
float y;
}Po;
//3.typedef和枚举
//定义枚举类型enum Weather
enum Weather {
sun,
rainy,
haze
};
//方式一:
typedef enum Weather W;
//方式二:
typedef enum Wae {
s,
d,
f
} W1;
//方式三:
typedef enum {
w,
e,
r
} W2;
//4.typedef和函数指针
//给韩式指针取别名,注意:此处的MySum不是变量名,他是类型
typedef int (*MySum)(int a, int b);
int main() {
//定义整型变量a
NSInteger a = 20;
printf("a:%d\n",a);
//定义浮点型变量
WXFloat b = 3.14;
printf("b:%.2f\n",b);
MyFloat c = 3.24;
printf("c:%.2f\n",c);
string s = "28期";
printf("s:%s\n",s);
Point p = {1.3,2.4};
Py p1 = {1.2,2.3};
Po p2 = {1.4,5.6};
//函数指针
// int (*p)(int a, int b) = getSum;
//定义函数指针
MySum p3 = getSum;
int result = p3(2,3);
printf("result:%d\n",result);
return 0;
}
//定义求和函数
int getSum(int a, int b) {
return a + b;
}
C语言之typedef的使用
最新推荐文章于 2024-07-14 05:00:00 发布