1121518wo

         一、首先为什么会出现结构体呢?(这个问题得弄明白,学习的时候,要有打破砂锅问到底的精神,自己要多问个为什么是这样,把别人的东西变成自己的知识点)

                     在我们学习c语言的时候,,我们一般都会见到基本数据类型:int     char      float    double等等,但是在表示一些复杂的的数据,显然所学的基本数据类型是不够了,所以这个时候我们就引入结构体这种新的数据类型,来满足开发需要。

            二、什么是结构体?

           1、  简单的来说,结构体就是用户根据实际需求来定义自己的复合数据类型,我们还是来看一个例子:

 1 #include <stdio.h>
 2 
 3 //这里定义了一个一个新的数据类型为strcut Student ,并且这个结构体里面有三个成员,第一个占内存4个字节,第二个200个字节,第三个4个字节
 4 struct Student
 5 {
 6     int sid;
 7     char name[200];
 8     int age;    
 9 };//注意这里这个分号不要忘了写
10 
11 
12 int main(void)
13 {
14     //定义了一个数据类型为struct Student变量,名为st
15     struct Student st={1000, "haha",20
16     };   
17     //这里我们通过"." 来访问结构体里面的成员
18     printf("%d %s %d\n", st.sid, st.name, st.age);
19     return 0;
20     
21 }

 

运行结果如下:

     

2、接下来显示另外一种赋值方式,还是来看例子:

 1 #include <stdio.h>
 2 #include <string.h>
 3 struct Student
 4 {
 5     int sid;
 6     char name[200];
 7     int age;    
 8 }; 
 9 int main(void)
10 {
11 
12     struct Student st={1000, "haha",20
13     };   
14     printf("%d %s %d\n", st.sid, st.name, st.age);
15     //这里通过“.”访问结构体成员来进行赋值,其实和上面差不多,只不过形式不一样而已
16     st.sid=99;
17     strcpy(st.name,"lisi");//注意这里不能这样对字符类型的结构体成员赋值,st.name="lisi",这样会报错,我们只能用字符串函数strcpy()来进行连接字符串
18     st.age=21;
19     printf("%d %s %d\n", st.sid, st.name, st.age);
20     return 0;
21     
22 }

运行效果如下:

3、通过指针来访问结构体成员,为啥要这样呢;因为通过上面那种方式访问的话,它占用内存空间大(它至少要占用208个字节),会导致程序在运行的时候,效率低,好资源,所以呢,我们我接下来用指针来访问结构体成员,这样做,相对于刚才那种访问方式来说,它不耗内存资源(它只占用4个字节),运行效率高;好了我们来看例子吧。

 1 #include <stdio.h>
 2 #include <string.h>
 3 struct Student
 4 {
 5     int sid;
 6     char name[200];
 7     int age;    
 8 }; 
 9 int main(void)
10 {
11 
12     struct Student st={1000, "haha",20
13     };   
14      //这里定义了一个struct Student 类型的指针变量pst
15     struct Student * pst ;
16     pst=&st;
17     pst->sid=99;    //注意*pst=st,所以(*pst).sid等价于st.sid,所以pst->sid等价于st.sid
18     strcpy(pst->name,"lisi");
19     pst->age=21;
20     printf("%d %s %d\n",pst->sid,pst->name,pst->age);
21     
22     return 0;
23     
24 }

运行结果如下,我们可以看到和上面那种方式访问运行的结果一样:

        三、普通结构体变量和结构体变量做为函数形参来传递:

         我们还是来看一段代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 struct Student
 4 {
 5     int sid;
 6     char name[200];
 7     int age;    
 8 }; 
 9 void f(struct Student * pst);
10 int main(void)
11 {
12     struct Student st={1000, "haha",20
13     };   
14      //调用f()这个函数,注意这个形参是要传递地址的
15     f(&st);
16     printf("%d %s %d\n",st.sid,st.name,st.age);
17     return 0;
18 }
19 void f(struct Student * pst)
20 {
21     (*pst).sid=99;
22     strcpy(pst->name,"lisi");
23     pst->age=21;
24 }

      接着我们还可以对输出再进行函数包装输出:

 1 #include <stdio.h>
 2 #include <string.h>
 3 struct Student
 4 {
 5     int sid;
 6     char name[200];
 7     int age;    
 8 }; 
 9 void f(struct Student * pst);
10 void g(struct Student st);
11 int main(void)
12 {
13     struct Student st={1000, "haha",20
14     };   
15      
16     f(&st);
17     g(st);
18     return 0;
19 }
20 void g(struct Student st)
21 {
22      printf("%d %s %d\n",st.sid,st.name,st.age);
23 }
24 void f(struct Student * pst)
25 {
26     (*pst).sid=99;
27     strcpy(pst->name,"lisi");
28     pst->age=21;
29 }

最终输出的结果是:

 

         四、总结:

                1、结构体两种访问方式:

                          struct Student st ={10000,"lisi",20};

                          struct Student * pst;

                         a:      

                                   st.sid

                         b:

                                    pst->sid  (pst 所指向的结构体变量中的sid 这个成员

                    2、使用第一种访问方式比第二种指针访问访问效率低了很多,因为第一种方式它耗用大量的内存资源。

 

相关文章: