C07_C语言-结构体、枚举

1.定义结构体

#include <stdio.h>
/*
 * 构造类型:数组(数据类型单一)、结构体(数据类型可多种)
 */
void main() {
	//结构体声明(固定关键字) 结构体类型名(自定义名称)
	struct Person{
		int age;
		char *name;
	}p;	//同时声明一个结构体变量
	p.age = 90;
	p.name = "Personp";
	printf("%d, %s\n", p.age, p.name);

	//结构体  类型   结构体变量名
	struct Person pp = {35, "Ppp"};
	printf("%d, %s\n", pp.age, pp.name);

	//结构体声明(固定关键字) 结构体类型名(自定义名称)
	struct Human{
		int sex;
		char *country;
	}cn = {0, "CN"};	//同时声明一个结构体变量并初始化
	printf("%d, %s\n", cn.sex, cn.country);

	//另一个Human类型的结构体变量
	struct Human us = {1, "US"};
	printf("%d, %s\n", us.sex, us.country);

	//结构体声明
	struct {
		int weight;
		char *name;
	}jp;	//结构体变量。这一个结构体不能被重用,私有的
	jp.weight = 78;
	jp.name = "JustForP";
	printf("%d, %s\n", jp.weight, jp.name);

	//结构体声明
	struct {
		int weight;
		char *name;
	}plone = {.name = "LoneP"};	//私有属性的结构体变量。同时初始化一个属性
	printf("%s\n", plone.name);

	//结构体声明  结构体类型名
	struct Singer{
		int age;
		char *name;
	};

	//结构体  类型   结构体变量名
	struct Singer vigiles;	//声明
	vigiles.age = 28;		//初始化
	vigiles.name = "vigiles";
	printf("%d, %s\n", vigiles.age, vigiles.name);

	struct Singer eminem = {27, "Eminem"};	//定义。声明的同时初始化
	printf("%d, %s\n", eminem.age, eminem.name);

	struct Singer alizee = {.name = "Alizee"};	//仅初始化需要的属性
	printf("%d, %s\n", alizee.age, alizee.name);

	struct Singer mj;
	//mj = {55, "Michael"};	//错误的初始化方式!
	mj = alizee;
	mj.age = 55;
	printf("mj.age:%d, mj.name:%s\n", mj.age, mj.name);
}

2.作用域

#include <stdio.h>

//全局的。定义在main之前。生命从这一行开始,到本程序最后 一行结束
struct Human{
	int sex;
	char *country;
}h;	//这里可以同时声明一个变量

void main() {
	struct Human cn = {1, "Chinese"};	//没有局部的,就用全局的
	printf("%d, %s\n", cn.sex, cn.country);

	//局部的,尽管和全局的结构体类型相同,但不冲突
	struct Human{
		int sex;
		int color;
		char *country;
	};
	struct Human en = {1, 2, "English"};	//有局部的,就不用全局的
	printf("%d, %d, %s\n", en.sex, en.color, en.country);

	h.sex = 0;	//全局变量
	h.country = "France";
	printf("%d, %s\n", h.sex, h.country);

}

3.结构体和数组

	struct Human{
		int sex;
		int tooth;
		char *color;
		char *country;
	};

	struct Human humans[] = {
			{0, 32, "yellow", "CN"},
			{0, 28, "white", "En"},
			{1, 28, "white", "US"},
			{0, 28, "white", "US"},
			{1, 28, "white", "En"}
	};
	//printf("%d, %d, %d\n", sizeof(humans), sizeof(humans[0]), sizeof(humans)/sizeof(humans[0]));

	humans[2].color = "black";
	humans[2].country = "Ivory";

	int i;
	for(i = 0; i < sizeof(humans)/sizeof(humans[0]); i++){
		printf("%d\t%d\t%s\t%s\n", humans[i].sex, humans[i].tooth, humans[i].color, humans[i].country);
	}

4.结构体和指针

	struct Human{
		int sex;
		char *country;
	} tl = {2, "Thailand"};

	//结构体指针,指向一个结构体变量的地址
	struct Human *shle = &tl;

	printf("%d, %s\n", tl.sex, tl.country);
	printf("%d, %s\n", shle->sex, shle->country);
	printf("%d, %s\n", (*shle).sex, (*shle).country);

5嵌套

struct Date{
	int year;
	int month;
	int day;
};

void main() {
	struct Student{
		char * name;
		int sex;
		struct Date birthday;
		struct Country{
			char *province;
			char *addr;
		}country;
	} student = {
		"XiaoHong",
		1,
		{1987, 3, 2},
		{"Qinghai", "qinghai"}
	};

	printf("姓名:%s, \n性别:%d, \n生日:%d - %d - %d, \n家乡:%s - %s",
			student.name,
			student.sex,
			student.birthday.year,
			student.birthday.month,
			student.birthday.day,
			student.country.province,
			student.country.addr
			);
}

6.枚举

	//枚举声明(固定关键字)   枚举类型(自定义名称)
	enum Sex{
		man,	//取值。第一个是0
		woman	//实际是一个常量数字。第二个是1
	};
	//枚举  类型  变量
	enum Sex sex;
	sex = woman;

	int i;
	printf("输入0或1\n");
	scanf("%d", &i);
	switch(i){
		case man:
			printf("Man");
			break;
		case woman:
			printf("Woman");
			break;
		default:
			printf("xxx");
			break;
	}

- end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值