29._结构体

结构体
     为什么需要结构体
          为了表示一些复杂的事物,而普通的的基本
          类型无法满足实际要求

    什么叫结构体
          把一些基本数据类型组合在一起形成的一个
          新的复合数据类型,这个叫做结构体

    如何定义结构体
          三种方式,推荐使用第一种

 1 /*
 2     2015年04月26日 14:21:45
 3     目的:
 4         结构体的定义
 5 */
 6
 7 # include <stdio.h>
 8
 9 //第一种方式
10 struct Student1
11 {
12     int age;
13     float score;
14     char sex;
15 }; //分号不能省
16
17 //第二种方式
18 struct Student2
19 {
20     int age;
21     float score;
22     char sex;
23 }st2;
24
25 //第三种方式
26 struct
27 {
28     int age;
29     float score;
30     char sex;
31 }st3;
32
33 int main(void)
34 {
35
36     return 0;
37 }
38
39 /*
40     在VC6.0中运行结果是:
41 -----------------------------
42
43 -----------------------------
44     总结:
45
46
47 */

    怎么使用结构体变量
          赋值和初始化

 1 /*
 2     2015年04月26日 14:36:03
 3     目的:
 4         结构体的赋值和初始化
 5 */
 6
 7 # include <stdio.h>
 8
 9 struct Student
10 {
11     int age;
12     float score;
13     char sex;
14 };
15
16 int main(void)
17 {
18     struct Student st1 = {80, 66.6, ‘F‘}; //定义的同时初始化
19     struct Student st2; //先定义后初始化
20     st2.age = 10;
21     st2.score = 88;
22     st2.sex = ‘F‘;
23
24     printf("%d %f %c\n", st1.age, st1.score, st1.sex);
25     printf("%d %f %c\n", st2.age, st2.score, st2.sex);
26
27     return 0;
28 }
29
30 /*
31     在VC6.0中运行结果是:
32 -----------------------------
33 80 66.599998 F
34 10 88.000000 F
35 -----------------------------
36     总结:
37
38
39 */

    如何操作结构体变量中的成员
           1. 结构体变量名.成员名
           2. 指针变量名->成员名

 1 /*
 2     2015年04月26日 14:51:39
 3     目的:
 4         如何操作结构体变量中的成员
 5 */
 6
 7 # include <stdio.h>
 8
 9 struct Student
10 {
11     int age;
12     float score;
13     char sex;
14 };
15
16 int main(void)
17 {
18     struct Student st = {80, 66.6F, ‘F‘};
19     struct Student * pst = &st;
20
21     st.age = 22; //结构体变量名.成员名
22     pst->score = 99.9F;
23     /*指针变量名->成员名,这种格式较为常用
24     pst->age 在计算机内部会被转换成 (*pst).age 也等价于 st.age
25     99.9在C语言中默认是double类型,如果希望一个实数是float类型
26     必须在末尾加f或F,故99.9是double,99.9f或99.9F是float
27     */
28
29     printf("%d\n%f\n", st.age, pst->score);
30
31     return 0;
32 }
33
34 /*
35     在VC6.0中运行结果是:
36 -----------------------------
37 22
38 99.900002
39 -----------------------------
40     总结:
41
42
43 */

     结构体变量和结构体变量指针作为函数参数传递的问题

 1 /*
 2     2015年04月26日 16:02:47
 3     目的:
 4         结构体变量和结构体变量指针作为函数参数传递
 5 */
 6
 7 # include <stdio.h>
 8 # include <string.h>
 9 void InputStudent(struct Student *);
10 void OutputStudent(struct Student *);
11
12 struct Student
13 {
14     int age;
15     char sex;
16 }; //分号不能省
17
18 int main(void)
19 {
20     struct Student st;
21
22     InputStudent(&st); //对结构体变量输入,必须发送st的地址
23     //OutputStudent(st); //对结构体变量输出,可以直接发送st的内容
24     OutputStudent(&st); //也可以发送st的地址
25     return 0;
26 }
27
28 void InputStudent(struct Student * pStu) //pStu只占4个字节
29 {
30     (*pStu).age = 10;
31     pStu->sex = ‘F‘;
32 }
33
34 void OutputStudent(struct Student *pSt)
35 {
36     printf("%d %c \n", pSt->age, pSt->sex);
37 }
38
39 /*
40 void OutputStudent(struct Student ss)
41 {
42     printf("%d %c \n", ss.age, ss.sex);
43 }
44 */
45
46
47
48 /*
49     在VC6.0中运行结果是:
50 -----------------------------
51 10 F
52 -----------------------------
53     总结:涉及到对内容的更改,必须发送地址
54           如果只是访问,可以发送内容也可以发送地址
55           推荐发送地址,因为
56                 指针的优点:
57                     快速的传递数据
58                     耗用内存小
59                     执行速度快
60
61
62
63 */

     结构体变量的运算
           结构体变量不能相加、减、乘和除
           但可以相互赋值
           例如:
              st1 = st2;

     冒泡排序:

 1 /*
 2     2015年04月26日 16:30:30
 3     目的:
 4         冒泡排序
 5 */
 6
 7 # include <stdio.h>
 8
 9 //冒泡排序
10 void sort(int * a, int len)
11 {
12     int i, j, t;
13
14     for (i=0; i<len-1; ++i)
15     {
16         for (j=0; j<len-1-i; ++j)
17         {
18             if (a[j] > a[j+1]) // >升序,<降序
19             {
20                 t = a[j];
21                 a[j] = a[j+1];
22                 a[j+1] = t;
23             }
24         }
25     }
26 }
27
28 int main(void)
29 {
30     int a[6] = {10, 2, 8, -8, 11, 0};
31     int i;
32
33     sort(a, 6);
34
35     for (i=0; i<6; ++i)
36     {
37         printf("%d ", a[i]);
38     }
39
40     printf("\n");
41
42     return 0;
43 }
44
45 /*
46     在VC6.0中运行结果是:
47 -----------------------------
48 -8 0 2 8 10 11
49 -----------------------------
50     总结:
51
52 */

     举例
         动态构造存放学生信息的结构体数组

  1 /*
  2     2015年04月26日 17:20:47
  3     目的:
  4         学生管理系统
  5         动态构造一个数组,存放学生的信息
  6                 然后按分数排序输出(降序)
  7 */
  8
  9 # include <stdio.h>
 10 # include <malloc.h>
 11
 12 struct Student
 13 {
 14     int age;
 15     float score;
 16     char name[100];
 17 };
 18
 19 int main(void)
 20 {
 21     int len;
 22     struct Student * pArr;
 23     int i, j;
 24     struct Student t;
 25
 26     printf("请输入学生个数:\n");
 27     printf("len = ");
 28     scanf("%d", &len);
 29
 30     //动态构造一维数组
 31     pArr = (struct Student *)malloc(len * sizeof(struct Student));
 32
 33     for (i=0; i<len; ++i)
 34     {
 35         printf("请输入第%d个学生的信息:\n", i+1);
 36         printf("age = ");
 37         scanf("%d", &pArr[i].age);
 38
 39         printf("name = ");
 40         scanf("%s", pArr[i].name); //name是数组名,即首元素地址。不能再加&
 41
 42         printf("score = ");
 43         scanf("%f", &pArr[i].score);
 44
 45     }
 46
 47     //按学生成绩降序排序 冒泡算法
 48     for (i=0; i<len-1; ++i)
 49     {
 50         for (j = 0; j<len-1-i; ++j)
 51         {
 52             if (pArr[j].score < pArr[j+1].score)
 53             {
 54                 t = pArr[j]; //此处不能写成pArr[j].score,因为是整个一个学生信息互换,而不是只交换分数
 55                 pArr[j] = pArr[j+1];
 56                 pArr[j+1] = t;
 57             }
 58         }
 59
 60     }
 61
 62
 63     printf("\n\n\n\n");
 64
 65     //输出
 66     for (i=0; i<len; ++i)
 67     {
 68         printf("第%d个学生的信息是:\n", i+1);
 69         printf("age = %d\n", pArr[i].age);
 70         printf("name = %s\n", pArr[i].name);
 71         printf("score = %f\n", pArr[i].score);
 72
 73         printf("\n");
 74
 75     }
 76
 77
 78     return 0;
 79 }
 80
 81 /*
 82     在VC6.0中运行结果是:
 83 -----------------------------
 84 请输入学生个数:
 85 len = 3
 86 请输入第1个学生的信息:
 87 age = 22
 88 name = zhangsan
 89 score = 67
 90 请输入第2个学生的信息:
 91 age = 23
 92 name = lisi
 93 score = 77
 94 请输入第3个学生的信息:
 95 age = 34
 96 name = wangwu
 97 score = 56
 98
 99
100
101
102 第1个学生的信息是:
103 age = 23
104 name = lisi
105 score = 77.000000
106
107 第2个学生的信息是:
108 age = 22
109 name = zhangsan
110 score = 67.000000
111
112 第3个学生的信息是:
113 age = 34
114 name = wangwu
115 score = 56.000000
116 -----------------------------
117     总结:
118
119 */

时间: 2024-08-06 21:11:33

29._结构体的相关文章

Demo_张仕传_结构体考试-modify

/* 题目: //声明一个结构体类型 struct _AdvTeacher { char *name; char *tile; int age; char *addr; char *p1; //系统预留成员域 char **p2;//系统预留成员域 }; 要求定义一个结构体数组(6个元素),要求从键盘输入数据,并按照名称大小进行排序:打印输出. 1. 打印结构体数组,需要单独封装成函数:10 2. 排序结构体数组,需要单独封装成函数(按照名称进行排序):50 3. main函数中编写业务测试模型

C++_系列自学课程_第_12_课_结构体

1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 struct CDAccount 7 { 8 double balance; //余额 9 double rate; //利息 10 int term; //存期 11 }; 12 13 struct Date 14 { 15 int year; 16 int month; 17 int day; 18 }; 19 20 struct Per

经典算法_结构体

1 动态结构体 1 #define _CRT_SECURE_NO_WARNINGS 2 3 #include<stdio.h> 4 #include<stdlib.h> 5 6 struct dangdang 7 { 8 char email[30]; 9 char name[30]; 10 char addr[100]; 11 int num; 12 int bignum; 13 char tel[20]; 14 char phone[20]; 15 double rmb; 16

C编译器剖析_4.2 语义检查_表达式的语义检查(5)_结构体成员选择

4.2.5         成员选择运算符 在C语言中,结构体struct和联合体union被称为记录类型RecordType,在形如dt.a和ptr->a的后缀表达式中,运算符.和->被称为成员选择运算符.函数CheckMemberAccess()用于对这些表达式进行语义检查,与之相关的代码如图4.2.28所示.在表达式dt.a中,dt和a相当于是运算符.的两个操作数,dt对应的语法树结点的类型应是记录类型,图4.2.28第8行对此进行了判断:而在形如ptr->a的表达式中,ptr对应

全国计算机等级考试二级教程-C语言程序设计_第14章_结构体、共用体和用户定义类型

例14.2 对比向函数传递结构体数组名和向函数传递结构体变量名的区别. 1 #include<stdio.h> 2 3 typedef struct 4 { 5 int num; 6 double mark; 7 }REC; 8 9 void sub1(REC x) 10 { 11 x.num = 23; 12 x.mark = 81.5; 13 } 14 15 void sub2(REC y[]) 16 { 17 y[0].num = 12; 18 y[0].mark = 77.5; 19

C语言基础_结构体

一.结构体 1)声明 struct 用来声明结构体 作用:管理多个数据信息 struct student{ int num; //成员变量之间用;隔开 int age; char name[30]; float score; }Student;//分号不要忘记 2) 初始化 1.设置初始值使用{} 2.按照成员变量的顺序赋值 3.可以不设置信息,使用{0} Student stu1={1,18,"luofeng",90.0}; Student stu2={2,81,"sunj

C零散_摘自C程序设计_结构体中.和-&gt;区别

结构体操作中.和->的区别: 1 struct student { 2 char name[60]; 3 int age; 4 } 5 6 struct student stu1; 7 struct student *stu2; 8 9 stu2 = &stu1; 10 stu1.age = 10; 11 stu2->age = 10; 以上都是对stu1中的age赋值为10,所以.是面向于结构体变量操作符,->是面向于结构体变量指针的操作符.

结构体指针

结构体与指针 1.结构体指针的数值为某个结构体的内存空间的地址,从而指针指向某个结构体. 2.结构体指针的初始值为空(0),不指向任何结构体. 3.malloc操作:创建一个新的内存空间(从内存中选择一个内存空间存储结构体),p指向这个结构体,p的值为这个结构体的内存空间的地址. 4.结构体指针的赋值:一个结构体指针的数值等于另外一个结构体指针的数值,从而两个结构体指针指向相同的结构体.当对某个结构体进行访问时,选择的结构体指针只要满足该结构体指针指向这个结构体即可,即可以有很多种选择. 5.当

5月12日 结构体

一.结构体定义: 结构体一般定义在Main函数上面,位于Class下面,作为一个类:一般情况Struct定义在Main函数前面,Main函数里面的地方都可以使用,参数前面加上public代表公用变量. 二.格式: struct +结构体的名称 { public int+变量名; public string+变量名: public int+变量名: } namespace _5月12日_结构体 { class Program { struct student { public int num; p