欢迎来到天天文库
浏览记录
ID:42777623
大小:594.50 KB
页数:32页
时间:2019-09-22
《c语言 结构体》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、第二讲结构与链表主要内容结构类型与结构变量的定义结构变量的引用与初始化结构数组指向结构类型数据的指针链表─结构指针的应用定义已有类型的别名结构类型的引入000001AngelF19851020000001958090结构类型定义struct结构类型名{数据类型数据项1;数据类型数据项2;…………数据类型数据项n;};structdate//日期结构类型{intyear;intmonth;intday;};structstd_info//学生信息结构类型{charno[7];charname[9];ch
2、arsex[2];structdatebirthday;};structscore//成绩结构类型{charno[7];intscore1;intscore2;intscore3;};000001AngelF19851020000001958090定义学生基本信息的结构类型,存储学生的相关信息(学号,姓名,性别,出生日期,三门课的成绩)☆“结构类型名”和“数据项”的命名规则,与变量名相同。数据类型相同的数据项,既可逐个、逐行分别定义,也可合并成一行定义。本例中的日期结构类型,也可改为如下形式:struct
3、date{intyear,month,day;};结构类型中的数据项,既可以是基本数据类型,也允许是另一个已经定义的结构类型本例中的结构类型std_info,其数据项“birthday”就是一个已经定义的日期结构类型date将1个数据项称为结构类型的1个成员(或分量)。structdate//日期结构类型{intyear;intmonth;intday;};structyg_info//员工信息类型{charno[6];charname[9];charsex[2];structdatebirthday;c
4、harzw[9];charmar[2];floatpay;};structpo_info//配偶信息类型{charname[9];charsex[2];structdatebirthday;charaddress[20];};structbb_info//子女信息类型{charname[9];charsex[2];structdatebirthday;charborn[20];};定义结构类型:员工信息:职工号,姓名,性别,出生日期,职务,婚否,工资员工配偶信息:姓名,性别,出生日期,工作单位员工子女信息
5、:姓名,性别,出生日期,出生地structdate{intyear,month,day;};structbase_info{charname[9];charsex[2];structdatebirthday;};structyg_info//员工信息类型{charno[6];structbase_infoinfo;charzw[9];charmar[2];floatpay;};structpo_info//配偶信息类型{structbase_infoinfo;charaddress[20];};struc
6、tbb_info//子女信息类型{structbase_infoinfo;charborn[20];};定义结构类型:员工信息:职工号,姓名,性别,出生日期,职务,婚否,工资员工配偶信息:姓名,性别,出生日期,工作单位员工子女的信息:姓名,性别,出生日期,出生地structbb_infobaby;baby.bornbaby.info.namebaby.info.birthday.year结构变量的定义结构类型与结构变量是两个不同的概念间接定义法structstd_info{charno[7];charna
7、me[9];charsex[2];structdatebirthday;};structstd_infostudent;直接定义法structstd_info{charno[7];charname[9];charsex[2];structdatebirthday;}student;例:利用定义的结构类型structstd_info,定义结构变量,存储和显示一个学生的基本情况。#include"struct.h"//定义并初始化一个外部结构变量studentstructstd_infostu={"00010
8、2",“angel",“f",{1980,9,20}};voidmain(){printf("%s",stu.no);printf("%s",stu.name);printf("%s",stu.sex);printf("%d-%d-%d",stu.birthday.year,stu.birthday.month,stu.birthday.day);}//struct.h文件structdate{intyear
此文档下载收益归作者所有