1 / 20

第 10 章 结构 体

第 10 章 结构 体. 10.1 问题的提出 10.2 用 typedef 命名类型 10.3.1 结构体类型 10.3.2 定义结构体类型变量 10.3.3 引用结构体变量 10.3.4 结构体变量的初始化 10.4.1 定义结构体数组 10.4.2 结构体数组的初始化 10.4.3 结构体数组应用举例 10.5 结构体指针. 10.1 问题的提出. 如何把一个学生的学号( num )、姓名( name )、性别( sex )、年龄( age )、成绩( score )、家庭地址( address )等数据项有机地组织起来 ?

shino
Download Presentation

第 10 章 结构 体

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第10章 结构体 10.1 问题的提出 10.2 用typedef命名类型 10.3.1 结构体类型 10.3.2 定义结构体类型变量 10.3.3 引用结构体变量 10.3.4 结构体变量的初始化 10.4.1 定义结构体数组 10.4.2 结构体数组的初始化 10.4.3 结构体数组应用举例 10.5 结构体指针

  2. 10.1 问题的提出 如何把一个学生的学号(num)、姓名(name)、性别(sex)、年龄(age)、成绩(score)、家庭地址(address)等数据项有机地组织起来? 办法是定义新的数据类型sutdent,如下所示。 struct student { int num; char name[20]; char sex; int age; float score; char address[30]; };

  3. 10.2 用typedef命名类型 typedef是C语言中经常用的关键字,可用来重命名数据类型。 typedef int INTEGER; 指定用INTEGER代表int类型。这样,以下两行语句等价: int i, j; INTEGER i, j; 又如 typedef int* intp; intp p,q; 请问:p,q是什么类型的变量?

  4. 10.3.1 结构体类型 声明一个结构体类型的一般形式为: struct 结构体名 { 成员列表; }; 例如, struct student { int num; char name[20]; char sex; };

  5. 10.3.2 定义结构体类型变量 有4种方式 1. 先声明结构体类型再定义变量 例如首先声明结构体类型struct student: struct student { int num; char name[20]; char sex; int age; float score; char address[30]; }; 然后再定义结构体类型struct student的变量: struct student std1, std2;

  6. 2. 使用typedef声明 例如首先声明结构体类型struct student: struct student { int num; char name[20]; char sex; int age; float score; char address[30]; }; typedef struct student stud; 然后再定义结构体类型stud的变量: stud std1, std2;

  7. 3. 在声明类型的同时定义变量 struct student { int num; char name[20]; char sex; int age; float score; char address[30]; } std1, std2;

  8. 4. 不指定类型名而直接定义结构体类型变量 struct { int num; char name[20]; char sex; int age; float score; char address[30]; } std1, std2;

  9. 10.3.3 引用结构体变量 (1)相同类型的结构体变量可以互相赋值,例如, std1 = std2; 但是不能将一个结构体变量作为一个整体进行输入和输出,只能对结构体变量中的各个成员分别进行输入和输出。 引用结构体变量中成员的方式为: 结构体变量名.成员名 例如,std1.num表示std1变量中的num成员。可以对变量的成员赋值,例如, std1.num = 10001; “.”是成员运算符,它是优先级最高的运算符之一。

  10. (2)如果成员本身又属于一个结构体类型,则要用若干个成员运算符,一级一级地找到最低的一级的成员。只能对最低级的成员进行赋值或存取以及运算。(2)如果成员本身又属于一个结构体类型,则要用若干个成员运算符,一级一级地找到最低的一级的成员。只能对最低级的成员进行赋值或存取以及运算。

  11. (3)对结构体变量中的成员可以像普通变量一样进行各种运算。例如,std1.age++;(3)对结构体变量中的成员可以像普通变量一样进行各种运算。例如,std1.age++; (4)可以引用结构体变量成员的地址,也可以引用结构体变量的地址。例如, scanf("%d", &std1.num); // 输入std1.num的值 printf("%0x", &std1); // 以16进制输出std1的起始地址

  12. 10.3.4 结构体变量的初始化 【例10-1】 对结构体变量进行初始化。 #include <stdio.h> struct student { int num; char name[20]; char sex; char address[30]; }; typedef struct student stud;

  13. int main() { stud std = {10001, "Li Jun", 'M', "Kunming"}; printf("No.: %d\nname: %s\nsex: %c\naddress: %s\n", std.num, std.name, std.sex, std.address); return 0; }

  14. 结构体数组的定义、初始化及应用1 【例10-2】 对候选人得票进行统计。假设有3个候选人,选民每次输入一个候选人的姓名,要求最后输出各人得票结果。 struct Candidate { char name[20]; int count; }; typedef struct Candidate Cand; int main() { int i, j; char name[20]; Cand candidates[3] ={{"Li", 0}, {"Zhang", 0}, {"Wang", 0}};

  15. 结构体数组的定义、初始化及应用2 for (i = 1; i <= 10; i++) { printf("please input candidate's name: "); scanf("%s", &name); for (j = 0; j < 3; j++) { if (strcmp(name, candidates[j].name) == 0) { candidates[j].count++; } } }

  16. 结构体数组的定义、初始化及应用3 printf("\nresult: \n"); for (i = 0; i < 3; i++) { printf("%s: %d\n", candidates[i].name, candidates[i].count); } return 0; }

  17. 程序输入及运行结果: please input candidate's name: Li please input candidate's name: Wang please input candidate's name: Li please input candidate's name: Li please input candidate's name: Zhang please input candidate's name: Wang please input candidate's name: Li please input candidate's name: Wang please input candidate's name: Wang please input candidate's name: Li result: Li: 5 Zhang: 1 Wang: 4

  18. 10.5.1 指向结构体变量的指针变量1 【例10-3】 通过指向结构体变量的指针输出该结构体变量的信息。 #include <stdio.h> #include <string.h> struct student { int num; char name[20]; char sex; double score; }; typedef struct student stud;

  19. 10.5.1 指向结构体变量的指针变量2 void main() { stud std; // 定义struct student类型的变量 stud *stdptr; // 定义指针变量 stdptr = &std; std.num = 10001; strcpy(std.name, "Li Jun"); std.sex = 'M'; std.score = 89.5; printf("num: %d\nname: %s\nsex: %c\nscore: %f\n", stdptr->num, stdptr->name, stdptr->sex, stdptr->score); }

  20. 第15次上机作业 10.1 (1),(2),(3) 10.2 (1),(2) 10.3 (1)

More Related