结构体

我们要处理一组事物的某一类属性时,可以用数组,但是当我们要描述一组事物的的不同特点是,没有哪个类型可以表示各种特点,这时就引进了结构体的概念,它是用户自定义的新型数据类型,你可以在结构体中添加你所需要的数据类型,可以是相同的,也可以是不同的,把这些数据类型组合起来描述事物的一堆信息。

【1】定义结构体:

struct 结构体类型名
{
数据类型   成员名1;
数据类型   成员名2;

数据类型   成员名n;
};

1 struct ak{        //ak:类型名 
2 int id;              //id:成员名
3 int score;
4 char name[5];    
5 
6 };

 

【2】定义结构体变量

struct 结构体名
{
成员列表;
}变量名1;

struct 结构体名 变量名2;

 

【3】访问结构体成员

结构体变量名.成员名


【4】结构体初始化操作

struct 结构体名 变量名={初始数据表};

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4     struct ak{                    //ak:类型名 
 5             int id;            //id:成员名
 6             int score;
 7             char name[5];        
 8     
 9     }s1, s3 = {12, 99, "kkk"};        //s1:变量名     初始化s3
10 int main(int argc, const char *argv[])
11 {
12     s1.id = 10;
13     s1.score = 84;
14     strcpy(s1.name, "pxl");
15 
16     printf("id = %d\n", s1.id);
17     printf("score = %d\n", s1.score);
18     printf("name = %s\n", s1.name);
19 
20     struct ak s2;
21 
22     s2.id = 11;
23     s2.score = 79;
24     strcpy(s2.name, "fgh");
25     
26     printf("id = %d\n", s2.id);
27     printf("score = %d\n", s2.score);
28     printf("name = %s\n", s2.name);
29 
30     printf("id = %d\n", s3.id);
31     printf("score = %d\n", s3.score);
32     printf("name = %s\n", s3.name);
33     
34     return 0;
35 }

 

【5】无名结构体
没有类型名。
一般状态下:无名结构体不可以定义局部变量。

 

【6】结构体嵌套
1--- 成员所属的数据类型是 结构体类型
2--- 内部的结构体通常定义为无名结构体

 

 1 #include <stdio.h>
 2 
 3 struct A{
 4         int id;
 5         float score;
 6         struct {                //建议内部使用无名结构体
 7             int year;
 8             int month;
 9         }d;
10     }s = {2, 88.44, {2016, 11}};       //最好加上内部{}
11 
12 int main(int argc, const char *argv[])
13 {
14     printf("id = %d, score = %.1f, year = %d, month = %d\n", s.id, s.score, s.d.year, s.d.month);
15     
16     s.id = 1;
17     s.score = 99.99;
18     s.d.year = 2016;
19     s.d.month = 7;
20 
21     printf("id = %d, score = %.1f, year = %d, month = %d\n", s.id, s.score, s.d.year, s.d.month);
22 
23     return 0;
24 }

 

【7】结构体数组
1--- 本质是数组,元素是结构体类型

 

 1 #include <stdio.h>
 2 
 3 
 4     struct ak{                    //ak:类型名 
 5             int id;            //id:成员名
 6             int score;
 7             char name[8];        
 8     
 9     }s[3] = {{1, 81, "p1"},
10             {2, 82, "p2"},
11             {3, 83, "p3"}};
12 int main(int argc, const char *argv[])
13 {
14     int i, j = 0;
15 
16     for (i=0; i<3; i++)
17     {
18         printf("id = %d  ", s[i].id);
19         printf("score = %d  ", s[i].score);
20         printf("name = %s\n", s[i].name);
21     }
22     
23     
24 
25     for(i=0; i<3; i++)
26     {
27         if(scanf("%d%d%s", &s[i].id, &s[i].score, s[i].name) != 3 || s[i].name[0] == '#')
28         {
29             puts("end");
30             break;
31             
32         }
33         j++;
34     }
35     
36     for(i=0; i<j; i++)
37     {
38         printf("id = %d  ", s[i].id);
39         printf("score = %d  ", s[i].score);
40         printf("name = %s\n", s[i].name);
41 
42     }
43     return 0;
44 }

 

posted @ 2016-09-27 20:11  星星之火pxl  阅读(162)  评论(0编辑  收藏  举报