一.结构类型
1.枚举
(1).枚举是一种用户定义的数据类型,它用关键字enum。以如下语句
enum 枚举类型名称 {名字0,...名字n};
枚举的名称并不经常的使用,要用的是大括号里的名字,因为它们就是常量符号,类型为int值从0~n。
例:enum color{red,yellow,green}
则,创建了三个变量,red的值为0,yellow的值为1,green的值为2。但需要一下可以排列起来的常量值时候,定义枚举的意义就是给了这些常量值名字。
(2).枚举量可以作为值,可以作为int来输出和输入;枚举类型可以跟上enum作为类型,但是实际上是以整数来做内部计算和外部输入输出的
例
#include<stdio.h>
enum color {red, yello, green};
void f(enum color c);
int main(void){
enum color t=red;
scanf("%d",&t);
f(t);
return 0;
}
void f(enum color c){
printf("%d\n",c);
}
(3).套路:自动计数的枚举;需要遍历所有的枚举量或者需要建立一个用枚举量做下标的数组的时候就就很方便。
#include<stdio.h>
enum color {red, yellow, green, numcolors};
int main(int argc,char* argv[]){
int color=-1;
char *colornames[numcolors]={
"red","yellow","green"
};
char *colorname = NULL;
printf("输入你最喜欢的颜色代码:");
scanf("%d",&color);
if(color>=0 && color<numcolors){
colorname=colornames[color];
}else{
colorname="unkonw";
}
printf("你喜欢的颜色是%s\n",colorname);
return 0;
}
(4).声明枚举量的时候可以指定量
enum color {RED=1,YELLOW=,GREEN=5};
#include<stdio.h>
enum color{red=1,yello,green=5,numcolors};
int main(int argc,char const *argv[]){
printf("code for green id %d\n",green);
return 0;
}
如果需要排比的符号量,用枚举比const int方便;而且枚举比宏好,因为枚举有int类型。
枚举主要用来定义符号量。
2.数据结构(结构声明一般放在所有函数外面,与函数同级,这样就可以被所有函数使用)
(1)声明结构的形式
struct point {
int x;
int y;
};
struct point p1,p2;
p1和p2都是point,里面有x和y的值;
等价形式:
struct point {
int x;
int y;
}p1,p2;
p1和p2都是point,里面有x和y的值。
(2).结构类型先声明,后定义。
例struct point p1,p2;
其中定义了变量p1,p2他们的的类型是point。
(3).结构的初始化
#include<stdio.h>
struct date{
int month;
int day;
int year;
}; //声明结构类型;
int main(int argc,char* argv[]){
struct date today={03,31,2019}; //定义了一个date类型的变量today,并且赋初始值;
struct date thismonth={.month=7,.day=31,.year=2019}; //赋初始值的第二种方式;
printf("today's date is %i-%i-%i.\n"
,today.year,today.month,today.day
);
printf("This monthis is %i-%i-%i.\n"
,thismonth.year,thismonth.month,thismonth.day
);
return 0;
}
注:1.结构成员:
结构和数组有点像:
数组用[ ]运算符和下标访问其成员
例如 a[0]=10;
结构用 . 运算符和名字访问成员变量
例如 today.day;
stduent.firstName
结构类型是虚的,只是告诉编译器制定一种模板,而结构变量是实体。
2.结构运算:赋值,取地址,传递函数参数。
例如:
1.p1=(struct point){5,10} //相当于p1.x=5;p1.y=10;
2.p1=p2; //相当于p1.x=p2.x;p1.y=p2.y;
#include<stdio.h>
struct date{
int month;
int day;
int year;
}; //声明结构类型;
int main(int argc,char* argv[]){
struct date today={03,31,2019}; //定义了一个date类型的变量today,并且赋初始值;
struct date thismonth;
//struct date thismonth={.month=7,.day=31,.year=2019}; //赋初始值的第二种方式;
thismonth=today;
thismonth.year=2014;
printf("today's date is %i-%i-%i.\n"
,today.year,today.month,today.day
);
printf("This monthis is %i-%i-%i.\n"
,thismonth.year,thismonth.month,thismonth.day
);
return 0;
}
3.结构指针
结构变量的名字并不是结构变量的地址,必须使用&运算符
struct date *pDate=&today;
3.结构作为函数参数
int number0fdays(struct date d)
- 整个结构可以作为参数的值传入函数
- 本质是在函数内新建一个结构变量,并复制调用者的结构的值
- 也可以返回一个结构
#include<stdio.h>
#include<stdbool.h>
struct date{
int month;
int day;
int year;
}; //声明结构类型;
bool isLeap(struct date d);//声明函数
int number0fDays(struct date d);//声明函数
int main(int argc ,char const *argv[])
{
struct date today ,tomorrow;
printf("Enter today's date (mm dd yyyy):");
scanf("%i %i %i",&today.month,&today.day,&today.year);
if(today.day != number0fDays(today)){
tomorrow.day=today.day+1;
tomorrow.month=today.month;
tomorrow.year=today.year;
}else if(today.month==12){
tomorrow.day=1;
tomorrow.month=1;
tomorrow.year=today.year+1;
}else{
tomorrow.day=1;
tomorrow.month=today.month+1;
tomorrow.year=today.year;
}
printf("Tomorrow's date is %i-%i-%i\n",tomorrow.month,tomorrow.day,tomorrow.year);
}
int number0fDays(struct date d) //定义函数
{
int days;
const int daysPerMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};//每月有几天
if(d.month==2&& isLeap(d))
days =29;
else
days=daysPerMonth[d.month-1];
return days;
}
bool isLeap(struct date d) //定义函数
{
bool leap=false;
if((d.year %4 == 0 && d.year %100 !=0 )||d.year%400 == 0) //闰年判断
leap = true;
return leap;
}
![](https://i-blog.csdnimg.cn/blog_migrate/99e15f13261e0970443d28ec8f4b900b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8f6d33d74164d72819dee98c20b038cc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/407199590d6957938e5e67432fa12a4f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f9fb11d7e7c59433bdeee60d7b01d837.png)