黑马程序员--C语言结构体

一、结构体和数组的区别:

数组:只能由多个相同类型的数据构成;

结构体:可以由多个不同类型的数据构成;

二、结构体定义的三种形式:

 1 int main()
 2 
 3 {       
 4 
 5          //结构体定义的3种形式
 6 
 7          //1. 定义结构体的时候声明变量
 8 
 9          struct Person
10 
11          {        // 里面的三个变量可以成为结构体的成员或者属性
12 
13                    double weight;
14 
15                    char *name;
16 
17                    int age;
18 
19                   
20 
21          }  stu,stu1; // 可以定义多个结构体变量,注意stu前面要有空格,后面有分号
22 
23  
24 
25          //2.单纯的只定义结构体
26 
27          struct Student
28 
29          {
30 
31                    int no;
32 
33          };// 注意此处的分号不可忘掉
34 
35          struct Student s;
36 
37  
38 
39          //3.没有结构体名称,直接定义变量
40 
41          struct
42 
43          {
44 
45                    int age;
46 
47          } p;

三、     结构体初始化

(1)初始化形式

         1>定义变量的时候初始化

        

1 struct Person pp = {10.2, "rose", 45};
2 printf("%d,%p", pp, &pp);
3 printf("年龄:%d, 姓名:%s, 体重:%f\n", pp.age, pp.name, pp.weight);

 

 

         2>先定义后赋值,结构体中给变量赋值用的是“.”

        

1 stu.age = 10;
2 stu.name = "jack";
3 stu.weight = 50;
4 printf("%p, %d", &stu, stu);
5 printf("年龄:%d, 姓名:%s, 体重:%f\n", stu.age, stu.name, stu.weight);

 

3>

1 struct Student s1 = {10, "jack", 40.0};
2 struct Student s2 = {11, "rose", 45.0};

 

//s2所有成员的值都对应的赋值给s1

s1 = s2;

(2)初始化常见错误

       

1  struct Student s2;
2 
3  s2 = {14, "zhs", 20.0};
4  
5  //类似的错误初始化:
6  
7  //int a[3];
8  
9  //a[3] = {1, 2, 3};

 

四、结构体内存分析

 1 void test1()
 2 
 3 {
 4 
 5          struct Student
 6 
 7          {
 8 
 9                    int age;    //4个字节
10 
11                    char a;      //1个字节
12 
13                    double weight; //8个字节
14 
15                    char *name;    //4个字节
16 
17          };
18 
19          struct Student stu;
20 
21          int s = sizeof(stu);
22 
23          printf("%d", s);
24 
25 }

 

(1)补齐算法:

1>

 1 struct Student
 2 
 3 {
 4 
 5          int age;    //4
 6 
 7          char a;               //1
 8 
 9          double weight;         //8   
10 
11 }
12 
13 //sizeof(stu)的结果:16

2>

 1 struct Student
 2 
 3 {
 4 
 5          int age;    //4
 6 
 7          double weight;         //8   
 8 
 9          char a;               //1
10 
11 }
12 
13 //sizeof(stu)的结果:24

3>

 1 struct Student
 2 
 3 {
 4 
 5          int age;    //4
 6 
 7          char a;               //1
 8 
 9          int height; //4
10 
11 }
12 
13 sizeof(stu)的结果:12

(2)结构体内存细节

1>定义结构体类型的时候(并不会分配存数空间)

2>定义结构体变量的时候(才是真正分配存储空间)

五、结构体数组

  1 #include <stdio.h>
  2 
  3 struct Date
  4 
  5 {
  6 
  7        int year;
  8 
  9        int month;
 10 
 11        int day;
 12 
 13 };
 14 
 15 struct Person
 16 
 17 {
 18 
 19        int age;
 20 
 21        char *name;
 22 
 23        struct Date birthday;
 24 
 25        /*注意结构体的嵌套定义中
 26 
 27        struct Person s;这种写法是错误的,自己不能嵌套自己
 28 
 29        */
 30 
 31 };
 32 
 33 void test(struct Person);
 34 
 35 void test2(struct Person *);
 36 
 37 int main()
 38 
 39 {
 40 
 41        int i;
 42 
 43        struct Person p[3] = { {12, "jack", {2002, 1, 3}}, {13, "rose", {2003, 2, 3}}, {14, "Mike", {2003, 2, 4}}};
 44 
 45        /*错误写法
 46 
 47        struct Person p[3];
 48 
 49        p[3] = { {12, "jack", {2002, 1, 3}}, {13, "rose", {2003, 2, 3}}, {14, "Mike", {2003, 2, 4}}};
 50 
 51       
 52 
 53        */
 54 
 55        //指向结构体的指针的定义
 56 
 57        struct Person *pp = p;
 58 
 59        /*
 60 
 61        指针取值:pp->age ==p.age
 62 
 63                        (*pp).age == p.age
 64 
 65        变量取值:p.age
 66 
 67        */
 68 
 69        for(i = 0; i<3; i++)
 70 
 71        {
 72 
 73               printf("%d, %s, %d-%d-%d\n", p[i].age, p[i].name, p[i].birthday.year, p[i].birthday.month, p[i].birthday.day);
 74 
 75        }
 76 
 77  
 78        for(i = 0; i<3; i++)
 79 
 80        {
 81 
 82               printf("%d, %s, %d-%d-%d\n", (pp+i)->age, (pp+i)->name, (*(pp+i)).birthday.year, (*(pp+i)).birthday.month, p[i].birthday.day);
 83 
 84        }
 85 
 86        test(p[0]);
 87 
 88        test2(&p[1]);
 89 
 90  return 0;
 91 
 92 }
 93 
 94 /*结构体与数组
 95 
 96 */ 
 97 
 98 void test(struct Person s)
 99 
100 {
101 
102        struct Date d = {2011,2,1};
103 
104        s.age = 20;
105 
106        s.name = "rose";
107 
108        s.birthday = d;
109 
110        printf("%d, %s, %d-%d-%d\n", s.age, s.name, s.birthday.year, s.birthday.month, s.birthday.day);
111 
112 }
113 
114  
115 void test2(struct Person *p)
116 
117 {
118 
119        struct Date d = {2011,2,1};
120 
121        p->age = 21;
122 
123        p->name = "point";
124 
125        p->birthday = d;
126 
127        printf("%d, %s, %d-%d-%d\n", p->age, p->name, p->birthday.year, p->birthday.month, p->birthday.day);
128 
129 }

六、结构体注意:

1>结构体类型不可重复定义

 1 //这种写法是错误的
 2 struct Student
 3 
 4 {
 5 
 6  int age;
 7 
 8 };
 9 
10 struct Student
11 
12 {
13 
14 char *name; 
15 
16 }

 

2>结构体可以嵌套定义,但是自己不能嵌套自己

 1 /*错误写法
 2 struct Student
 3 
 4 {
 5 
 6 int age;
 7 
 8 struct Student s;
 9 
10 };
11 */

这样写是不正确的

3>结构体初始化时不可以先定义后整体赋值

1 /*错误写法
2 struct Student s2;
3 
4 s2 = {14, "zhs", 20.0};
5 */

4>定义结构体类型的时候内存没有分配存储空间,当定义结构体变量的时候才分配;

5>取结构体中的变量的值得时候应用“.”符号,如果是指针应用“->”符号;

6>注意结构体的补齐算法.

 

posted @ 2014-12-22 15:49  zsling  阅读(134)  评论(0编辑  收藏  举报