- 4
- 0
- 约4.81千字
- 约 17页
- 2018-02-07 发布于河南
- 举报
第9章 结构体的基本概念
9.1.1 结构体与结构体类型的定义 9.1.1 结构体与结构体类型的定义 struct 结构体名 { 数据类型1 成员名1; 数据类型2 成员名2; …… 数据类型n 成员名n; }; 9.1.1 结构体与结构体类型的定义 例如学生类型的定义: struct student { char num[8]; /* 学号是字符数组类型 */ char name[30]; /* 姓名是字符数组类型 */ char sex; /* 性别是字符型 */ int age; /* 年龄是整型 */ char addr[60]; /* 住址是字符数组类型 */ int score[6]; /* 成绩是整型数组类型 */ }; 9.1.2 结构体变量的定义 利用已定义的结构体类型名定义变量 struct 结构体名 变量名表; 例如: struct student t1, t2, s[30] ; 9.1.2 结构体变量的定义 9.1.2 结构体变量的定义 在定义结构体类型的同时定义变量 9.1.2 结构体变量的定义 直接定义结构体类型变量 9.1.2 结构体变量的定义 struct date { int year, month, day;}; struct date today={2009,12,24}; struct student { char num[8], name[20], sex; struct date birthday; float score; }a; 9.1.2 结构体变量的定义 【例】结构体数组的初始化。 struct s { char num[8],name[20],sex; float score; }stu[3]={{9606011,Li ming,M,87.5}, {9606012,Zhang jiangguo,M,79}, {9606013,Wang ping,F,90}}; 9.1.3 结构体变量的使用 sizeof计算结构体变量所占内存空间 9.1.3 结构体变量的使用 结构体变量成员引用的一般形式: 结构体变量名.成员名 9.3 结构体指针变量 结构体指针变量定义的一般形式: struct 结构体名 *指针变量名; 9.3 结构体指针变量 用结构体变量名的引用形式: d.year d.month d.day 9.3 结构体指针变量 9.3 结构体指针变量 【例】分析自增自减运算对程序结果的影响。 struct code { int i; char c; }a[ ]={{100,A},{200,B}, {300,C},{400,D}}; struct code *p=a; 9.3 结构体指针变量 main( ) {struct code *p=a;//p=a[0]; printf(%d\t,++p-i); printf(%c\t,(++p)-c); printf(%d\t,(p++)-i); printf(%c\t,++p-c); printf(%d\t,p-i++); printf(%d\n,p-i); } 结构体练习 1、程序输出结果是: struct ord {int x,y;}dt[2]={1,2,3,4}; main() { struct ord *p=dt; printf(“%d”,++p-x); printf(“%d”,(++p)-y); } 2、能给w中year成员赋值1980的语句是: struct workers {int num;char name[20];char c; struct {int day;int month;int year;}s; }; struct workers w,*pw; pw=w; A)*pw.year=1980; B)w.year=1980; C)pw-year=1980; D)w.s.year=1980; * 结构(或结构体)是分量数目固定、类型允许不同、排列有序的一种用户自定义构造类型。 是一种必须要程序员自行定义,然后才能使用
原创力文档

文档评论(0)