AutoLeaders控制组——结构体学习笔记

结构体

一.枚举

 #include <stdio.h>

enum color { red,yellow,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);
}
  • 枚举量可以作为值
  • 枚举类型可以跟上enum作为类型
  • 实际上是以整数来做内部计算和外部输入输出的

因为枚举是从零开始递增,所以可以在最后一个常量名称后再放一个number,number所代表的常数就是枚举的数量

声明枚举量的时候可以指定值
enum COLOR {RED=1,YELLOW,GREEN=5};

二.结构类型

struct date {
	int month;
	int day;
	int year;
};       //勿忘最后面的分号!
  • 和本地变量一样,在函数内部声明的结构类型只能在函数内部使用
  • 所以通常在函数外部声明结构类型,这样就可以被多个函数所使用了

    1.第一个是上面声明结构名字叫point下面定义变量p1和p2
    2.第二个是没有声明结构名字point,直接定义了变量p1和p2(比较不常见)
    3.第三个将变量名称声明和定义变量放在了一起
struct date {
	int month;
	int day;
	int year;
};
	struct date today = {07,31,2014};
	struct date thismonth = {.month=7, .year=2014}; //若没赋值则为0,即day为0

结构成员

  • 结构和数组有点像
  • 数组用[]运算符和下标访问器成员
  • a[0] = 10;
  • 结构用.运算符和名字访问其成员
  • today.day
  • pl.x
  • pl.y

结构运算

  • 要访问整个结构,直接用结构变量的名字
  • 对于整个结构,可以做赋值,取地址,也可以传递给函数参数
  • p1 = (struct point){5,10}; //相当于p1.x = 5; p1.y = 10;
  • p1 = p2; //相当于p1.x = p2.x;p1.y =p2. y;
    数组不能做这两种计算

结构指针

  • 与数组不同,结构变量的名字并不是结构变量的地址,必须使用&运算符
  • struct date *pDate = &today;

三.结构与函数
结构作为函数参数

int numberofdays(struct date d)
  • 整个结构可以作为参数的值传入函数
  • 这时候是在函数内新建一个结构变量,并复制调用者的结构的值
  • 也可以返回一个结构
  • 这与数组完全不同

判断明天的年月日

 #include <stdio.h>
 #include <stdbool.h>
 
 struct date{
 	int month;
 	int year;
 	int day;
 };
 
 bool isleap(struct date d);
 int numberofdays(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 != numberofdays(today)){
		tomorrow.day = today.day+1;         //如果不是一个月最后一天,日+1
		tomorrow.month = today.month;
		tomorrow.year = today.year;
	}else if (today.month == 12){           //一年最后一个月则年+1,月,日变3为1
		tomorrow.day = 1;
		tomorrow.month = 1;
		tomorrow.year = today.year+1;
	}else{                                  //不是最后一个月则月+1,日变为1    
		tomorrow.day = 1;
		tomorrow.month = today.month+1;
		tomorrow.year = today.year;
	}
	
	printf("Tomorrow's date is %i-%i-%i.\n",tomorrow.year, tomorrow.month, tomorrow.day);
	
	return 0;
}

int numberofdays(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;
}

解决传值的方案:

  • 可以在函数内自己定义一个结构变量让这个变量接收值,然 后在函数结尾将这个结构变量传回去;
  • 将结构指针传进去;

用->表示指针所指的结构变量中的成员

四.结构中的结构

结构类型数组例子
在这里插入图片描述
在这里插入图片描述
即一次定义了5个struct time结构 放在struct time的数组testTimes中

结构中的变量可以是基础类型int,float,double也可以是另外一个结构

struct dateAndTime {
struct date sdate;
struct time stime;
};

嵌套的结构
在这里插入图片描述
结构中的结构的数组
在这里插入图片描述

五.类型定义
typedef

typedef int Length;  //即让Length成为int类型的别名
//就可以这样使用
Length a,b,len;
Length number[10];

可用来简化复杂的名字
typedef和最后一个单词中间所有东西命名为最后一个单词,typedef只会让最后一个单词代替原来类型

六.联合
union中的所以成员变量连起来用一个空间叫做联合

 #include<stdio.h>
 
 typedef union{
 	int i;
 	char ch[sizeof(int)];
 } CHI;
 
 int main(int argc, char const *argv[])
 {
 	CHI chi;
 	int i;
 	chi.i = 1234;
 	for (i=0; i<sizeof(int); i++){
 		printf("%02hhX",chi.ch[i]);
	 }
	 printf("\n");
	 
	 return 0;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值