黑马程序员——C语言中的结构体

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

一、什么是结构体

当一个整体由多个数据构成时,我们可以用数组来表示这个整体,但是数组有个特点:内部的每一个元素都必须是相同类型的数据。

在实际应用中,我们通常需要由不同类型的数据来构成一个整体,比如学生这个整体可以由姓名、年龄、身高等数据构成,这些数

据都具有不同的类型,姓名可以是字符串类型,年龄可以是整型,身高可以是浮点型。

 为此,C语言专门提供了一种构造类型来解决上述问题,这就是结构体,它允许内部的元素是不同类型的。


二、结构体定义

1.定义形式

 结构体内部的元素,也就是组成成分,我们一般称为"成员"。

结构体的一般定义形式为:

 struct 结构体名{

类型名1 成员名1;

类型名2 成员名2;   

};

2.举例

比如,我们定义一个学生类

[objc]  view plain copy
  1. struct  Student{  
  2.     charchar *name; // 姓名  
  3.     int no; // 学号  
  4.     float score; // 分数  
  5. };  


三、结构体变量的定义

前面只是定义了名字为Student的结构体类型,并非定义了一个结构体变量,就像int一样,只是一种类型。

定义结构体的方式一般有三种

1.先定义结构体类型,再定义变量

[objc]  view plain copy
  1. struct  Student{  
  2.     charchar *name;   
  3.     int no;   
  4.     float score;   
  5. };  
struct Student stu;

   
 

2.定义结构体类型的同时定义变量

[objc]  view plain copy
  1. struct  Student{  
  2.     charchar *name;   
  3.     int no;   
  4.     float score;   
  5. }stu;  

3.直接定义结构体类型变量,省略类型名

[objc]  view plain copy
  1. struct  stu{  
  2.     charchar *name;   
  3.     int no;   
  4.     float score;   
  5. }stu;  


四、结构体的注意点

1.结构体不可以包含自己

[objc]  view plain copy
  1. struct Student {  
  2.     int age;  
  3.     struct Student stu;  
  4. };  

2.结构体内可以包含别的结构体

[objc]  view plain copy
  1. struct Date {  
  2.      int year;  
  3.      int month;  
  4.      int day;  
  5. };  
  6.   struct Student {  
  7.      charchar *name;  
  8.      int no;  
  9.      float score;  
  10.      struct Date birthday;  
  11. };  


3.只有当定义属于结构体类型的变量时,系统才会分配存储空间给该变量

[objc]  view plain copy
  1. struct Student {  
  2.     charchar *name;  
  3.     int age;  
  4. };//没有分配存储空间  
  5. struct Student stu;//分配了内存空间给结构体变量  

 4.结构体占用的字节数和结构体内部的成员变量有关


五、结构体的初始化

将各成员的初值,按顺序地放在一对大括号{}中,并用逗号分隔,一一对应赋值。

比如初始化Student结构体变量stu

[objc]  view plain copy
  1. struct  Student{  
  2.     charchar *name;   
  3.     int no;   
  4.     float score;   
  5. };  
  6. struct Student stu = {"ZM",110,120};  

结构体只能在定义变量的时候赋初值

[objc]  view plain copy
  1. struct Student stu = {"ZM",110,120};  

[objc]  view plain copy
  1. /**************************************************************************** 
  2.  *题目: 
  3.  *1:结构体的初步练习,定义并输出结构体 
  4.  ****************************************************************************/  
  5.   
  6. #include <stdio.h>  
  7.   
  8. int main()  
  9. {  
  10.     struct Person{  
  11.         int age;  
  12.         double height;  
  13.         charchar *name;  
  14.     };  
  15.     struct Person p = {20,1.75,"ZhangMing"};  
  16.       
  17.     p.age = 23;  
  18.     p.name = "XieQing";  
  19.     p.height = 1.73;  
  20.       
  21.     printf("age=%d,name=%s,height=%.2f\n",p.age,p.name,p.height);  
  22.     return 0;  
  23. }  

[objc]  view plain copy
  1. /**************************************************************************** 
  2.  * 
  3.  *题目: 
  4.  *复习定义结构体的三种方式 
  5.  *使用循环语句输出结构体 
  6.  ****************************************************************************/  
  7.   
  8. //struct Student{  
  9. //    int age;  
  10. //};  
  11. //struct Student stu;  
  12. //  
  13. //struct Student{  
  14. //    int age;  
  15. //}stu;  
  16. //struct Student stu2;  
  17. //  
  18. //struct {  
  19. //    int age;  
  20. //}stu;  
  21.   
  22. int main()  
  23. {  
  24.     struct RankRecord {  
  25.         int no;  
  26.         charchar *name;  
  27.         int score;  
  28.     };  
  29.       
  30.     struct RankRecord records[3] = {  
  31.         {1,"jack",100},  
  32.         {2,"jims",98},  
  33.         {3,"jake",89}  
  34.     };  
  35.       
  36.     records[0].name = "lily";  
  37.       
  38.     for (int i=0; i<3; i++)  
  39.     {  
  40.         printf("%d\t%s\t%d\n",records[i].no,records[i].name,records[i].score);  
  41.     }  
  42.       
  43.     return 0;  
  44. }  

[objc]  view plain copy
  1. /**************************************************************************** 
  2.  *题目: 
  3.  *1.指向结构体的指针的定义 
  4.  *struct Student *p; 
  5.  *2.利用指针访问结构体的成员 
  6.  *1> (*p).成员名称 
  7.  *2> p->成员名称 
  8.  ****************************************************************************/  
  9.   
  10. #include <stdio.h>  
  11.   
  12. int main()  
  13. {  
  14.     struct Student{  
  15.         int no;  
  16.         int age;  
  17.     };  
  18.       
  19.     struct Student stu = {1,20};  
  20.     struct Student *p = &stu;  
  21.     p->no = 10;  
  22.       
  23.     printf("age=%d, no=%d\n",stu.age,stu.no);  
  24.     printf("age=%d, no=%d\n",(*p).age,(*p).no);  
  25.     printf("age=%d, no=%d\n",p->age,p->no);  
  26.     printf("age=%d, no=%d\n",(*p).age,(*p).no);  
  27.       
  28.     return 0;  
  29. }  


[objc]  view plain copy
  1. /**************************************************************************** 
  2.  * 
  3.  *题目: 
  4.  *结构体嵌套 
  5.  ****************************************************************************/  
  6.   
  7. #include <stdio.h>  
  8.   
  9. int main()  
  10. {  
  11.     struct  Date{  
  12.         int year;  
  13.         int month;  
  14.         int day;  
  15.     };  
  16.       
  17. //    这个要注意下  
  18.     struct Student {  
  19.         int no;  
  20.         struct Date birthday;  
  21.         struct Date ruxueday;  
  22.     };  
  23.       
  24.     struct Student stu = {1,{2000,1,1},{2012,2,3}};  
  25.     printf("%d,(%d,%d,%d),(%d,%d,%d)\n", stu.no,  
  26.            stu.birthday.year, stu.birthday.month, stu.birthday.day,  
  27.            stu.ruxueday.year, stu.ruxueday.month, stu.ruxueday.day);  
  28.       
  29.     return 0;  
  30. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值