结构体和共用体

结构体

声明结构和结构体变量

#include<stdio.h>
#include<string.h>
#define MAXTITL 41
#define MAXAUTL 31
#define MAX 100

/*结构声明:并未创建实际的数据对象:也称结构声明为模板*/
struct book{		//book:标记名(可选)	
	char title[MAXTITL];
	char name[MAXAUTL];
	float value;
/*定义结构变量*/
}bok;
int main() 
{
	struct book bk1;	//声明并创建了一个结构体变量
	struct book *pbk;	//声明一个指向 struct类型的结构的指针	 
}

 复合字面常量

  bk1=(struct book){"sting","steing",2.1};

伸缩数组成员

struct flex{
	int count;
	double average;
	double scores[];	//伸缩型数组成员 
};  
	struct flex *pf1,*pf2;
	*pf2=*pf1	
	//1,不能用带伸缩型数组成员的结构进行赋值或拷贝:这样只能拷贝除伸缩型数组成员以外的其他成员 
	//2,不能以按值方式把这种结构传递给结构。要把结构的地址传递给函数
	//3.不要使用带伸缩型数组成员的结构作为数组成员或另一个结构成员
}

初始化

#include<stdio.h>
struct STudent{
	char ST_name[20];
	char ST_sex;
	int ST_grade;
}student1={"Hanxue",'W',3};
int main()
{
	struct STudent student2={"Wangyongsheng",'M',3};

	printf("第一个学生:\n");
	printf("名字:%s\n",student1.ST_name);
	printf("性别:%c\n",student1.ST_sex);
	printf("年级:%d\n",student1.ST_grade);
	printf("第二个学生:\n");
	printf("名字:%s\n",student2.ST_name);
	printf("性别:%c\n",student2.ST_sex);
	printf("年级:%d\n",student2.ST_grade);

    struct STudent student3={.ST_grade=50};	//指定初始化器 
}

结构体数组定义及初始化

#include<stdio.h>
struct STudent{
	char name[20];			//姓名
	int number;			//学号
	char sex;			//性别
	int grede;			//年级
}student[5]={
	{"zhangsan",101,'w',3},
	{"wangwu",102,'m',4},
	{"lisi",103,'w',5},
	{"yeye",104,'m',6},
	{"liyeye",105,'w',7}
};
int main()
{
	int i;
	for(i=0;i<5;i++)
	{
		printf("第%d个学生:\n",i+1);
		printf("名字:%s\n学号:%d\n性别:%c\n年级:%d\n\n",student[i].name,student[i].number,student[i].sex,student[i].grede);
	}
	return 0;
}

结构体指针

/*

*/
#include<stdio.h>
#include<string.h>
struct STudent{
	char name[20];			//姓名
	int number;			//学号
	char sex;			//性别
	int grede;			//年级
}student1;

int main()
{
	struct STudent *pST,*pST2,student2;				//定义结构体指针
	pST=&student1;	//指针指向结构体变量
	pST2=&student2;
	//利用指向运算符
	strcpy(student1.name,"wangyongsheng");
	student1.number=1000;
	(*pST).sex='w';
	(*pST).grede=4;
	strcpy(pST2->name,"wangyongzhuo");
	pST2->number=1002;
	pST2->sex='w';
	pST2->grede=3;
	printf("学生信息\n");
	printf("利用成员操作运算符.\n");
	printf("名字:%s\n学号:%d\n性别:%c\n年级:%d\n",(*pST).name,(*pST).number,(*pST).sex,(*pST).grede);
	printf("名字:%s\n学号:%d\n性别:%c\n年级:%d\n",pST2->name,(*pST2).number,(*pST2).sex,(*pST2).grede);

}

指向结构体数组的指针

#include<stdio.h>
struct STudent{
	char name[20];			//姓名
	int number;			//学号
	char sex;			//性别
	int grede;			//年级
}student[5]={
	{"zhangsan",101,'w',3},
	{"lisi",102,'m',4},
	{"wangwu",103,'w',5},
	{"liyeye",104,'m',6},
	{"huaha",105,'w',7}
};
int main()
{
	struct STudent *pST;
	int i;
	pST=student;
	for(i=0;i<5;i++,pST++)
	{
		printf("第%d个学生:\n",i+1);
		printf("名字:%s\n学号:%d\n性别:%c\n年级:%d\n",pST->name,pST->number,pST->sex,pST->grede);

	}
	return 0;
}

包含结构的结构

#include<stdio.h>
struct date{
	int year;			//年
	int month;			//月
	int day;			//日
};
struct stuent{
	char name[30];		//姓名
	int num;			//学号
	char sex;				//性别
	struct date birthday;		//出生日期
}student={"li叶叶",10000,'w',{1998,3,14}};
int main()
{
	printf("学生信息:\n");
	printf("姓名:%s\n",student.name);
	printf("学号:%d\n",student.num);
	printf("性别:%c\n",student.sex);
	printf("出生日期:%d-%d-%d\n",student.birthday.year,student.birthday.month,student.birthday.day);
	return 0;
}

使用结构体变量作为函数的参数

使用结构体变量作为函数实参数时,采取的是“值传递”方式,即会将结构体变量所占内存单元的内容全部顺序出传递给形参,
形参也必须是同类型的结构体变量。
注:在形参位置使用结构体变量,但是在函数调用期间,形参也要占用内存单元。这种传递方式在空间和时间上开销比较大,
另外,如果在函数与内部修改了变量中成员的值,则改变的值不会返回到主调函数中

#include<stdio.h>
#include<string.h>
struct Student{
	char name[20];			//姓名
	float score[3];			//分数
}student={"wangyongsheng",98.5f,89.0,93.5f};
void Display(struct Student stu);
int main()
{
	Display(student);
	printf("名字:%s\n语文:%.2f\n数学:%.2f\n英语:%.2f\n",student.name,student.score[0],student.score[1],student.score[2]);
	printf("平均成绩:%.2f\n",(student.score[0]+student.score[1]+student.score[2])/3);
	
	return 0;
}
void Display(struct Student stu)
{
	printf("学生信息\n");
	printf("名字:%s\n语文:%.2f\n数学:%.2f\n英语:%.2f\n",stu.name,stu.score[0],stu.score[1],stu.score[2]);
	printf("平均成绩:%.2f\n",(stu.score[0]+stu.score[1]+stu.score[2])/3);
	strcpy(stu.name,"zhanglingxiang");
	stu.score[0]=0;
	stu.score[1]=0;
	stu.score[2]=0;
	printf("名字:%s\n语文:%.2f\n数学:%.2f\n英语:%.2f\n",stu.name,stu.score[0],stu.score[1],stu.score[2]);
	printf("平均成绩:%.2f\n",(stu.score[0]+stu.score[1]+stu.score[2])/3);
	
}

使用指向结构体变量的指针作为函数的参数

在传递结构体变量的指针时,只是将结构体变量的首地址进行传递,并没有将变量的副本进行传递。
注:因为传递的是变量的地址,如果在函数中改变成员中数据看,那么返回主调函数的变量会发生变化

#include<stdio.h>
#include<string.h>
struct Student{
	char name[20];
	float score[3];
}student={"Wangyongsheng",98.5f,89.0,93.5f};
void Display(struct Student *stu);							//声明结构同时定义变量
int main()
{
	Display(&student);
	printf("名字:%s\n语文:%.2f\n数学:%.2f\n英语:%.2f\n",student.name,student.score[0],student.score[1],student.score[2]);
	return 0;
}
void Display(struct Student *stu)
{
	printf("学生信息\n");
	printf("名字:%s\n语文:%.2f\n数学:%.2f\n英语:%.2f\n",(*stu).name,(*stu).score[0],stu->score[1],stu->score[2]);
	printf("改变后值:\n");
	strcpy((*stu).name,"王永胜");
	(*stu).score[0]=0;
	stu->score[1]=0;
	stu->score[2]=0;
	printf("名字:%s\n语文:%.2f\n数学:%.2f\n英语:%.2f\n",(*stu).name,(*stu).score[0],stu->score[1],stu->score[2]);
}

 共用体

定义共用体类型的一般形式:
union 共用体名
{
    成员列表
  }变量列表;

1.内存段可以用来存放几种不同类型的成员,但是每次只能存放其中的一种,而不能同时存放所有的类型。
也就是说,在共用体中,只有一个成员起作用,其他成员不起作用
2.共用体变量中起作用的成员是最后一次存放的成员。在存入一个新的成员后,原有的成员失去作用。
3.共用体变量的地址和他的各成员的地址是一样的。
4.不能对共用体变量名赋值,也不能企图引用变量名来得到一个值

#include<stdio.h>
union Dataunion{
	int a;
	char c;				//共用体也称联合体,它使几种不同类型的变量存放到同一段内存单元中,所以共用体在同一时间只能有一个值,它属于某一个数据成员,由于所有成员位于同一块内存,因此共用体的大小就等于最大成员的大小。
}variable;
int main()
{
	union Dataunion fit;		//Dataunion类型的联合变量 
	union Dataunion save[10];	//内含10个联合变量的数组 
	union Dataunion *pu;		//指向Dataunion 类型的联合变量的指针 
	union Dataunion un={97};			//对共用体初始化时,只需要一个初始化值就足够了,其类型必须和共用体的第一个成员一致
	printf("a=%d\n",un.a);
	printf("c=%c\n",un.c);
	
	un.c='A';
	printf("a=%d\n",un.a);
	printf("c=%c\n",un.c);
	
	return 0;
}

枚举

一个枚举变量包含一组相关的标识符,其中每个标识符都对应一个整数值,称为枚举常量

#include<stdio.h>
enum Color{red=1,blue,green}color;			//定义枚举变量,并初始化
int main()
{
	int a;
	printf("请输入一个数:\n");
	scanf("%d",&a);
	switch(a)
	{
	case red:printf("这是红色\n");break;
	case blue:printf("这是蓝色\n");break;
	case green:printf("这是绿色\n");break;
	default:printf("????\n");break;
	}

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值