C语言结构体(struct)常见使用方法(转)

本文转自 CSDN huqinweI987

基本定义:结构体,通俗讲就像是打包封装,把一些有共同特征(比如同属于某一类事物的属性,往往是某种业务相关属性的聚合)的变量封装在内部,通过一定方法访问修改内部变量。

 结构体定义:

第一种:只有结构体定义

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

 
  1. struct stuff{  
  2.         char job[20];  
  3.         int age;  
  4.         float height;  
  5. };  

第二种:附加该结构体类型的“结构体变量”的初始化的结构体定义

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

 
  1. //直接带变量名Huqinwei  
  2. struct stuff{  
  3.         char job[20];  
  4.         int age;  
  5.         float height;  
  6. }Huqinwei;  

也许初期看不习惯容易困惑,其实这就相当于:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct stuff{  
  2.         char job[20];  
  3.         int age;  
  4.         float height;  
  5. };  
  6. struct stuff Huqinwei;  

 

第三种:如果该结构体你只用一个变量Huqinwei,而不再需要用

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct stuff yourname;  

去定义第二个变量。

那么,附加变量初始化的结构体定义还可进一步简化出第三种

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct{  
  2.         char job[20];  
  3.         int age;  
  4.         float height;  
  5. }Huqinwei;  

把结构体名称去掉,这样更简洁,不过也不能定义其他同结构体变量了——至少我现在没掌握这种方法。

 

结构体变量及其内部成员变量的定义及访问:

绕口吧?要分清结构体变量和结构体内部成员变量的概念。

就像刚才的第二种提到的,结构体变量的声明可以用:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct stuff yourname;  

其成员变量的定义可以随声明进行:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct stuff Huqinwei = {"manager",30,185};  

也可以考虑结构体之间的赋值:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.         struct stuff faker = Huqinwei;  
  2. //或    struct stuff faker2;  
  3. //      faker2 = faker;  
  4. 打印,可见结构体的每一个成员变量一模一样  

 

如果不使用上边两种方法,那么成员数组的操作会稍微麻烦(用for循环可能好点)

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. Huqinwei.job[0] = 'M';  
  2. Huqinwei.job[1] = 'a';  
  3. Huqinwei.age = 27;  
  4. nbsp;Huqinwei.height = 185;  

 

结构体成员变量的访问除了可以借助符号".",还可以用"->"访问(下边会提)。

 

指针和数组:

 

这是永远绕不开的话题,首先是引用:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct stuff *ref = &Huqinwei;  
  2. ref->age = 100;  
  3. printf("age is:%d ",Huqinwei.age);  

 

指针也是一样的

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

 
  1. struct stuff *ptr;  
  2. ptr->age = 200;  
  3. printf("age is:%d ",Huqinwei.age);  

 

结构体也不能免俗,必须有数组:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct test{  
  2.         int a[3];  
  3.         int b;  
  4. };  
  5. //对于数组和变量同时存在的情况,有如下定义方法:  
  6.         struct test student[3] =      {{{66,77,55},0},  
  7.                                         {{44,65,33},0},  
  8.                                         {{46,99,77},0}};  
  9. //特别的,可以简化成:  
  10.         struct test student[3] =       {{66,77,55,0},  
  11.                                         {44,65,33,0},  
  12.                                         {46,99,77,0}};  

 

变长结构体

可以变长的数组

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <string.h>  
  4. typedef struct changeable{  
  5.         int iCnt;  
  6.         char pc[0];  
  7. }schangeable;  
  8.   
  9. main(){  
  10.         printf("size of struct changeable : %d ",sizeof(schangeable));  
  11.   
  12.         schangeable *pchangeable = (schangeable *)malloc(sizeof(schangeable) + 10*sizeof(char));  
  13.         printf("size of pchangeable : %d ",sizeof(pchangeable));  
  14.   
  15.         schangeable *pchangeable2 = (schangeable *)malloc(sizeof(schangeable) + 20*sizeof(char));  
  16.         pchangeable2->iCnt = 20;  
  17.         printf("pchangeable2->iCnt : %d ",pchangeable2->iCnt);  
  18.         strncpy(pchangeable2->pc,"hello world",11);  
  19.         printf("%s ",pchangeable2->pc);  
  20.         printf("size of pchangeable2 : %d ",sizeof(pchangeable2));  
  21. }  

 

运行结果

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. size of struct changeable : 4  
  2. size of pchangeable : 4  
  3. pchangeable2->iCnt : 20  
  4. hello world  
  5. size of pchangeable2 : 4  

 

结构体本身长度就是一个int长度(这个int值通常只为了表示后边的数组长度),后边的数组长度不计算在内,但是该数组可以直接使用。

(说后边是个指针吧?指针也占长度!这个是不占的!原理很简单,这个东西完全是数组后边的尾巴,malloc开辟的是一片连续空间。其实这不应该算一个机制,感觉应该更像一个技巧吧

 

结构体嵌套:

结构体嵌套其实没有太意外的东西,只要遵循一定规律即可:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. //对于“一锤子买卖”,只对最终的结构体变量感兴趣,其中A、B也可删,不过最好带着  
  2. struct A{   
  3.         struct B{  
  4.              int c;  
  5.         }  
  6.         b;  
  7. }  
  8. a;  
  9. //使用如下方式访问:  
  10. a.b.c = 10;   

特别的,可以一边定义结构体B,一边就使用上:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. struct A{  
  2.         struct B{  
  3.                 int c;  
  4.         }b;  
  5.   
  6.         struct B sb;  
  7.   
  8. }a;  

使用方法与测试:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.         a.b.c = 11;  
  2.         printf("%d ",a.b.c);  
  3.         a.sb.c = 22;  
  4.         printf("%d ",a.sb.c);  
  5. 结果无误。   

 

结构体与函数:

关于传参,首先:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. void func(int);  
  2. func(a.b.c);  

把结构体中的int成员变量当做和普通int变量一样的东西来使用,是不用脑子就想到的一种方法。

另外两种就是传递副本和指针了 :

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. //struct A定义同上  
  2. //设立了两个函数,分别传递struct A结构体和其指针。  
  3. void func1(struct A a){  
  4.         printf("%d ",a.b.c);  
  5. }  
  6. void func2(struct A* a){  
  7.         printf("%d ",a->b.c);  
  8. }  
  9. main(){  
  10.         a.b.c = 112;  
  11.         struct A * pa;  
  12.         pa = &a;  
  13.         func1(a);  
  14.         func2(&a);  
  15.         func2(pa);  
  16. }   

占用内存空间:

 

struct结构体,在结构体定义的时候不能申请内存空间,不过如果是结构体变量,声明的时候就可以分配——两者关系就像C++的类与对象,对象才分配内存(不过严格讲,作为代码段,结构体定义部分“.text”真的就不占空间了么?当然,这是另外一个范畴的话题)。

 

结构体的大小是结构体所含变量大小的总和,并且不能用"char a[]"这种弹性(flexible)变量,必须明确大小,下面打印输出上述结构体的size:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1.         printf("size of struct man:%d ",sizeof(struct man));  
  2.         printf("size:%d ",sizeof(Huqinwei));  
  3. 结果毫无悬念,都是28:分别是char数组20,int变量4,浮点变量4.   

 

和C++的类不一样,结构体不可以给结构体内部变量初始化,。

 

如下,为错误示范:

 

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. #include<stdio.h>  
  2. //直接带变量名Huqinwei  
  3. struct stuff{  
  4. //      char job[20] = "Programmer";  
  5. //      char job[];  
  6. //      int age = 27;  
  7. //      float height = 185;  
  8. }Huqinwei;  

 

PS:结构体的声明也要注意位置的,作用域不一样。 

C++的结构体变量的声明定义和C有略微不同,说白了就是更“面向对象”风格化,要求更低。

 

那么熟悉了常用方法,都要注意哪些常犯错误呢,见C语言结构体常见错误

点击查看原文:http://blog.csdn.net/huqinwei987/article/details/23625823

We're here to put a dent in the universe!code改变世界!-----------------------------------------------------by.不装b就无法活下去的晨曦
原文地址:https://www.cnblogs.com/firstaurora/p/5122421.html