浅谈结构体

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

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

            2、什么是结构体?spa

           一、  简单的来讲,结构体就是用户根据实际需求来定义本身的复合数据类型,咱们仍是来看一个例子:指针

 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 }

 

运行结果以下:code

     

二、接下来显示另一种赋值方式,仍是来看例子:blog

 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 }

运行效果以下:内存

三、经过指针来访问结构体成员,为啥要这样呢;由于经过上面那种方式访问的话,它占用内存空间大(它至少要占用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 }

运行结果以下,咱们能够看到和上面那种方式访问运行的结果同样:开发

        3、普通结构体变量和结构体变量作为函数形参来传递:字符串

         咱们仍是来看一段代码:

 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 }

最终输出的结果是:

 

         4、总结:

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

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

                          struct Student * pst;

                         a:      

                                   st.sid

                         b:

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

                    二、使用第一种访问方式比第二种指针访问访问效率低了不少,由于第一种方式它耗用大量的内存资源。

相关文章
相关标签/搜索