16.结构体_A【郝斌C语言入门学习】

数组:解决大量同类型数据的存储的问题。

结构体

实例:

 1 /*
 2     复杂的事物包含的信息较多,基本数据类型已经不能满足复杂数据的存储。
 3     学生信息(性别,年龄,分数)
 4 */
 5 # include <stdio.h>
 6 //定义结构体,形成新的数据类型
 7 struct Student
 8 {
 9     char sex;
10     int age;
11     float score;
12 };
13 int main(void)
14 {
15 /*  char sex1;
16     int age1;
17     float score1;
18     char sex2;
19     int age2;
20     float score2;
21 */    
22     struct Student st = {'F', 20, 65.6};//定义struct Student 数据类型,st定义结构体变量。    
23     printf("姓名 = %c,年龄 = %d,分数 = %f\n", st.sex, st.age, st.score);
24     st.sex = 'F';
25     st.age = 25;
26     st.score = 100;
27     printf("姓名 = %c,年龄 = %d,分数 = %f\n", st.sex, st.age, st.score);
28     return 0;
29 }

1.为什么需要结构体

  为了表示一些复杂的事物,而普通的基本类型无法满足实际要求。

2.什么叫结构体

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

3.如何定义结构体 

  struct Student //这只是定义了一个新的数据类型,并没有定义变量
  {
      char sex;
      int age;
      float score;
  };
4.怎么去使用结构体变量
  (1)赋值和初始化
    1)定义的同时可以整体赋初值
    2)如果定义完后,则只能单个的赋初值
  (2)如何取出结构体变量中的每一个成员【重点】
    1)结构体变量名.成员名
    2)指针变量名->成员名(这种方式最常用)
     指针变量名->成员名 在计算机内部会被转化成 (*指针变量名).成员名的方式来执行所以说这两种方式时等价的
例题1:
 1 # include <stdio.h>
 2 struct Student
 3 {
 4     int age;
 5     float score;
 6     char sex;
 7 };
 8 int main(void)
 9 {
10     //结构体变量初始化方法一
11     struct Student student_One = {25, 72.5F, 'T'};//初始化,定义的同时赋初值
12     //结构体变量初始化方法二
13     struct Student student_two;
14     student_two.age = 30;
15     student_two.score = 85.0F;
16     student_two.sex = 'F';
17     printf("%d  %f  %c\n", student_One.age, student_One.score, student_One.sex);
18     //结构体变量名.成员可以取出结构体变量中的每一个成员
19     printf("%d  %f  %c\n", student_two.age, student_two.score, student_two.sex);
20     return 0;
21 }
例题2:取结构体变量中成员的方法
 1 # include <stdio.h>
 2 struct Student
 3 {
 4     int age;
 5     float score;
 6     char sex;
 7 };
 8 int main(void)
 9 {
10     struct Student st = {80, 66.6, 'F'};
11     st.age = 10;//第一种方式
12     struct Student * pst = &st;//&st不能改成st
13     pst->age = 99;//第二种方式
14     //1.pst->age在计算机内部会被转化成(*pst).age,没有为什么,这就是->含义,这也是一种硬性规定。
15     //2.pst->age <=> (*pst).age <=> st.age
16     //3. 我们之所以知道pst->age 等价于 st.age, 是因为pst->age是被转化成了(*pst).age
17     printf("%d %f %c\n", pst->age, pst->score, pst->sex);
18     return 0;
19 }
总结:1.pst->age在计算机内部会被转化成(*pst).age,没有为什么,这就是->含义,这也是一种硬性规定。
   2.pst->age <=> (*pst).age <=> st.age
   3.我们之所以知道pst->age 等价于 st.age, 是因为pst->age是被转化成了(*pst).age 
   4.pst->age的含义:pst所指向的那个结构体变量中的age这个成员 
例题3:
 1 # include <stdio.h>
 2 struct Student
 3 {
 4     int age;
 5     float score;
 6     char sex;
 7 };
 8 int main(void)
 9 {
10     struct Student st = {80, 66.6F, 'F'};
11     st.age = 10;//第一种方式
12     struct Student * pst = &st;//&st不能改成st
13     pst->age = 99;//第二种方式
14     //1.pst->age在计算机内部会被转化成(*pst).age,没有为什么,这就是->含义,这也是一种硬性规定。
15     //2.pst->age <=> (*pst).age <=> st.age
16     //3. 我们之所以知道pst->age 等价于 st.age, 是因为pst->age是被转化成了(*pst).age
17     st.score = 66.6F;
18     //第一种方式,66.6在C语言中默认是double类型,如果希望一个实数是float类型,则必须在末尾加f或F,因此66.6是double,66.6f或66.6F是float
19     printf("%d %f %c\n", pst->age, pst->score, pst->sex);
20     return 0;
21 }
22 /*
23     --------------------------------------------
24     在vc++6.0中的输出结果:99 66.599998 F
25     --------------------------------------------
26 */
复习:
 1 # include <stdio.h>
 2 //定义一个结构体数据类型
 3 struct Student
 4 {
 5     int age;
 6     char sex;
 7     char name[100];
 8 };//分号不能省略
 9 int main(void)
10 {
11     struct Student st = {20, 'F', "小军"};//用结构体数据类型定义一个结构体变量
12     struct Student * pst = &st;
13     printf("%d %c %s\n", st.age, st.sex, st.name);
14     printf("%d %c %s\n", pst->age, pst->sex, pst->name);
15     //pst->age <=> (*pst).age <=> st.age
16     return 0;
17 }
  (3)结构体变量和结构体变量指针作为函数参数传递的问题
    推荐使用结构体指针变量作为函数参数来传递
例题1:通过函数实现结构体变量的输入和输出
 1 # include <stdio.h>
 2 # include <string.h>
 3 void InputStudent(struct Student);
 4 struct Student
 5 {
 6     int age;
 7     char sex;
 8     char name[100];
 9 };//分号不能省略
10 int main(void)
11 {
12     struct Student st;
13     
14     InputStudent(st);//对结构体变量的输入
15     printf("%d %c %s\n", st.age, st.sex, st.name);
16     
17     //OutputStudent();//对结构体变量的输出
18     
19     return 0;
20 }
21 //本函数无法修改主函数st的值
22 void InputStudent(struct Student stu)
23 {
24     stu.age = 10;
25     stu.sex = 'F';
26     strcpy(stu.name, "张三");
27 }
28 /*
29 ----------------------------------------------
30     在vc++6.0中的输出结果:1973316885  Ca
31 ----------------------------------------------
32 */
例题1:通过函数实现结构体变量的输入和输出
 1 # include <stdio.h>
 2 # include <string.h>
 3 void InputStudent(struct Student *);
 4 void OutputStudent(struct Student );
 5 struct Student
 6 {
 7     int age;
 8     char sex;
 9     char name[100];
10 };//分号不能省略
11 int main(void)
12 {
13     struct Student st;
14     
15     InputStudent(&st);//对结构体变量的输入,修改st的值,必须要输入st的地址
16     printf("%d %c %s\n", st.age, st.sex, st.name);
17     
18     OutputStudent(st);//对结构体变量的输出,不修改st的值,可以发送st的值,也可以发送st的地址
19     
20     return 0;
21 }
22 //本函数无法修改主函数st的值
23 void InputStudent(struct Student * pstu)//pstu只占4个字节
24 {
25     pstu->age = 20;
26     pstu->sex = 'F';
27     strcpy(pstu->name, "李四");
28 }
29 void OutputStudent(struct Student  stu)
30 {
31     printf("%d %c %s\n", stu.age, stu.sex, stu.name);
32 }
33 /*
34 ----------------------------------------------
35     在vc++6.0中的输出结果:20 F 李四
36                            20 F 李四
37 ----------------------------------------------
38 */

指针的优点之一:快速的传递数据,耗用内存小,执行速度快。

  (4)结构体变量的运算
  结构体变量不能相加,不能相减,也不能相互乘除,但结构体变量可以相互赋值。
 1 # include <stdio.h>
 2 struct Student
 3 {
 4     int age;
 5     char sex;
 6     char name[100];
 7 };//分号不能省略
 8 int main(void)
 9 {
10     struct Student st1 = {20, 'T', "大刀王五"};
11     struct Student st2 = {18, 'F', "关芝琳"};
12     st1 = st2;
13     printf("%d %c %s\n", st1.age, st1.sex, st1.name);
14     return 0;
15 }
16 /*
17 ----------------------------------------------
18     在vc++6.0中的输出结果:18 F 关芝琳
19 ----------------------------------------------
20 */
  (5)结构体举例:动态构造存放学生信息的结构体数组


 

posted @ 2024-08-17 16:36  java帝国  阅读(20)  评论(0)    收藏  举报