1.
/*
typedef:给老的数据类型取别名;可以简写复杂的数据类型;
*/
typedef unsigned int ui; typedef int *pint;//pint是个类型,int *的新名字; typedef int (*pfunc)(int ,int );//pfunc是个类型,表示函数指针类型; int add(int a,int b) { return a+b; } int main(int argc, const char * argv[]) { ui a = 10;//用新的数据类型定义一个变量; printf("a=%u\n",a); int b=20; pint p = &b;//p是int *型 printf("*p=%d\n",*p); // pint q[3];//指针数组; pfunc pf = add;//pf是个变量 printf("add rst=%d\n",pf(a,b)); return 0; }
2.
/*
结构体:关键字 struct
*/
#include <string.h> struct student//定义了一种新的数据类型; { char name[20];//成员变量;存储空间说明:加入是一个鞋柜,char占20个字节,需要20个空间,int 需要4的字节空间,所以工需要28个空间; int age; int height; }; typedef struct student stu; typedef struct student//定义了一种新的数据类型; { char name[20];//成员变量; int age; int height; }stu; int main() { struct student xiaoming = {"xiaoming",18,188};//struct student是数据类型:xiaoming是变量名; printf("sizeof(struct student) is %lu\n",sizeof(struct student)); //访问结构体的成员变量 xiaoming.age=30; //1:改名字 strcpy(xiaoming.name,"wangcai"); //2:升级 strcat(xiaoming.name,"6s"); printf("name:%s,age:%d,height:%d\n",xiaoming.name,xiaoming.age,xiaoming.height); stu xiaohong = {"xiaohong",18,165}; printf("name:%s,age:%d,height:%d\n",xiaohong.name,xiaohong.age,xiaohong.height); return 0; }
#include <stdlib.h> typedef struct student//定义了一种新的数据类型; { char name[20];//成员变量; int age; int height; }stu; int main() { stu class[10]={}; stu *pst;//pst就是一个指针,stu是pst指向的数据类型;pst可以指向class里的任何一个元素; //给数组所在元素赋值 class[0].age = 18; //name :ios1518+2位学号 //height :180,175~180之间浮动 //age :15~24 for(int i=0;i<10;i++) { sprintf(class[i].name,"ios1518%02d",i+1); class[i].age = 15+i; class[i].height=random()%10+175;//random()表示是随机数;random()%10表示取0~9之间的任意数字; } //遍历学生信息 for(int i=0;i<10;i++) { printf("name=%s,age=%d,height=%d\n",class[i].name,class[i].age,class[i].height); } //按照身高排序:排序的主体是数组的元素,但key是元素的身高,排序需要将整个构造体全部元素进行排序 for(int i=0;i<10-1;i++) { for(int j=0;j<10-i-1;j++) { if(class[j].height>class[j+1].height) { stu temp = class[j]; class[j] = class[j+1]; class[j+1] = temp; } } } printf("========================================\n"); //遍历学生信息 for(int i=0;i<10;i++) { printf("name=%s,age=%d,height=%d\n",class[i].name,class[i].age,class[i].height); } //使用pst指针访问数组, // pst = &class[0];//指针指向首个元素 pst = class; printf("sizeof(stu)=%lu,sizeof(class)=%lu\n",sizeof(stu),sizeof(class)); //访问第一个元素 printf("first info ,name:%s,age:%d,height:%d\n",pst->name,pst->age,pst->height); printf("=========================================\n"); //遍历学生信息 for(int i=0;i<10;i++) { printf("name:%s,age:%d,height:%d\n",(pst+i)->name,(pst+i)->age,(pst+i)->height); } }
typedef struct student { char name[20]; int age; int height; union//联合体:二选一的关系 { int stuid; char nickname[20]; }; /* 共享同一段内存,按情况去解析; 内存大小按大的计算;//所以此结构体占用的内存是20+4+4+20=48字节; 初始化的时候,按照第一个解析; */ }stu1; int main() { stu1 xiaohong = {"xiaohong",20,165,0};//开始的时候只能按照int进行初始化,不能用nickname; strcpy(xiaohong.nickname, "旺财"); printf("%s\n",xiaohong.nickname); return 0; }
3.
/*
枚举:一枚一枚的举出来;把一个变量的合理取值列举出来;
枚举是整形常量,把变量的合理取值列举出来;
列举的常量,默认从0开始,后面一次加1;
*/
typedef enum weekday { Monday=1,//可设置,后面按照设置的值一次加1; Tuesday, Wednesday, Thursday, Firday, Saturday, Sunday }WEEKDAY; int main() { // enum weekday day; WEEKDAY day; printf("sizeof(day)=%lu\n",sizeof(day));//sizeof(day)=4,因为其实际保存的都是int类型; printf("Monday=%d\n",Monday); printf("Sunday=%d\n",Sunday); printf("请输入周几\n"); scanf("%d",&day); switch (day) { case Monday: printf("Monday\n"); break; case Tuesday: printf("Tuesday\n"); break; case Wednesday: printf("Wednesday\n"); break; case Thursday: printf("Thursday\n"); break; case Firday: printf("Firday\n"); break; case Saturday: printf("Saturday\n"); break; case Sunday: printf("Sunday\n"); break; default: printf("error\n"); break; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-06 11:49:23