C语言之typedef的使用

来源:互联网 发布:舒淇演技 知乎 编辑:程序博客网 时间:2024/04/29 02:53
#include<stdio.h>int  getSum(int a, int b);/*_____________________________typedef的使用______________________________*///1.typedef的作用//int取个一个别名NSIntegertypedef int  NSInteger;//float取个一个别名WXFloattypedef float  WXFloat;//在别名的基础上再取别名typedef WXFloat MyFloat;//2.typedef和指针typedef char * string;//3.typedef和结构体//定义结构体类型struct Mypointstruct 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 Weatherenum 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() {//定义整型变量aNSInteger 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;}

0 0
原创粉丝点击