目录(?)[+]

 

结构体定义和结构体变量的声明

方式一:

定义,初始化结构体变量分开

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     char name[10];  
  4.     char age;  
  5. };  
  6.   
  7. struct Student stu;  

 

方式二:

附加结构体变量初始化

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     char name[10];  
  4.     char age;  
  5. }stu;  


方式三:

[html]  view plain copy print ?  
 
  1. struct  
  2. {  
  3.     char name[10];  
  4.     char age;  
  5. }stu;  

把结构体名称去掉,这样更简洁,不过也不能定义其他同结构体变量了。

 

结构体内部成员变量

结构体内部成员变量的赋值:

方式一:

 

[html]  view plain copy print ?  
 
  1. struct Student stu = {"hyk", 25};  
  2. struct Student stu = {.age = 25, .name = "hyk"};  

 

方式二:

 

[html]  view plain copy print ?  
 
  1. struct Student stu;  
  2. strcpy(stu.name, "hyk");  
  3. stu.age = 25;  

 

 

 

 

 

方式三:

[html]  view plain copy print ?  
 
  1. struct Student stu = { "hyk", 25 };  
  2. struct Student stu2 = stu;// 实际是两个结构体内存的拷贝,将stu内存的值拷贝给了stu2,如果结构体成员有指针元素,那么就不能直接赋值。  

 

一个结构中可以有数组成员,也可以有指针成员,如果是指针成员结构体成员在初始化和赋值的时候就需要提前为指针成员分配内存。

 

指定结构体元素的位字段

定义一个结构体时可以指定具体元素的位长。

 

[html]  view plain copy print ?  
 
  1. struct Test{  
  2.     char a : 2;//指定元素为2位长,不是2个字节长  
  3. };  

 

 

结构体数组

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     int a[3];  
  4.     int b;  
  5. };  
  6.   
  7. int main() {  
  8.   
  9.     struct Student stu[3] = {  
  10.         { {1, 2, 3 }, 10 },  
  11.         { {4, 5, 6 }, 11 },  
  12.         { {7, 8, 9 }, 12 }  
  13.     };  
  14.   
  15.     // 可以简写为:  
  16.     struct Student stu[3] = {  
  17.         { 1, 2, 3, 10 },  
  18.         { 4, 5, 6, 11 },  
  19.         { 7, 8, 9, 12 }  
  20.     };  
  21.   
  22.     return 0;  
  23. }  

 

结构体与指针

 

[html]  view plain copy print ?  
 
  1. struct Student stu;  
  2. struct Student *p = &stu;  
  3. p->age = 26;  


 

 

 

 

 

结构体数组与指针

栈数组:

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     int a;  
  4.     int b;  
  5. };  
  6.   
  7. int main() {  
  8.   
  9.     struct Student stu[3];  
  10.     struct Student *p = stu;  
  11.   
  12.     int i;  
  13.     for (i = 0; i < 3; i++) {  
  14.         p->a = 10 + i;  
  15.         p->b = 20 + i;  
  16.         p++;  
  17.     }  
  18.   
  19.     printf("%d,%d\n", stu[0].a, stu[0].b);  
  20.     printf("%d,%d\n", stu[1].a, stu[1].b);  
  21.     printf("%d,%d\n", stu[2].a, stu[2].b);  
  22.     return 0;  
  23. }  

运行结果:

C——结构体总结_数组

堆数组:

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     int a;  
  4.     int b;  
  5. };  
  6.   
  7. int main() {  
  8.   
  9.     struct Student *p;  
  10.     p = malloc(sizeof(struct Student) * 3);  
  11.     struct Student *arr = p;//arr指针记录了p的初始指针  
  12.     p->a = 1;  
  13.     p->b = 2;  
  14.     p++;// 导致p指针的位置发生偏移,  
  15.     p->a = 3;  
  16.     p->b = 4;  
  17.       
  18.     printf("%d,%d\n", arr[0].a, arr[0].b);  
  19.     printf("%d,%d\n", arr[1].a, arr[1].b);  
  20.   
  21.     //free(p);// 因为p的指针发生偏移,free(p)会发生异常  
  22.     free(arr);// 可以正常释放malloc申请的堆内存  
  23.     return 0;  
  24. }  

运行结果:

C——结构体总结_初始化_02

 

结构体成员内存对齐图解

C——结构体总结_html_03

 

结构体嵌套对齐图解:

C——结构体总结_结构体数组_04


 

结构体定义和结构体变量的声明

方式一:

定义,初始化结构体变量分开

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     char name[10];  
  4.     char age;  
  5. };  
  6.   
  7. struct Student stu;  

 

方式二:

附加结构体变量初始化

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     char name[10];  
  4.     char age;  
  5. }stu;  


方式三:

[html]  view plain copy print ?  
 
  1. struct  
  2. {  
  3.     char name[10];  
  4.     char age;  
  5. }stu;  

把结构体名称去掉,这样更简洁,不过也不能定义其他同结构体变量了。

 

结构体内部成员变量

结构体内部成员变量的赋值:

方式一:

 

[html]  view plain copy print ?  
 
  1. struct Student stu = {"hyk", 25};  
  2. struct Student stu = {.age = 25, .name = "hyk"};  

 

 

方式二:

 

[html]  view plain copy print ?  
 
  1. struct Student stu;  
  2. strcpy(stu.name, "hyk");  
  3. stu.age = 25;  

 

 

 

 

 

方式三:

[html]  view plain copy print ?  
 
  1. struct Student stu = { "hyk", 25 };  
  2. struct Student stu2 = stu;// 实际是两个结构体内存的拷贝,将stu内存的值拷贝给了stu2,如果结构体成员有指针元素,那么就不能直接赋值。  

 

一个结构中可以有数组成员,也可以有指针成员,如果是指针成员结构体成员在初始化和赋值的时候就需要提前为指针成员分配内存。

 

指定结构体元素的位字段

定义一个结构体时可以指定具体元素的位长。

 

[html]  view plain copy print ?  
 
  1. struct Test{  
  2.     char a : 2;//指定元素为2位长,不是2个字节长  
  3. };  

 

 

结构体数组

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     int a[3];  
  4.     int b;  
  5. };  
  6.   
  7. int main() {  
  8.   
  9.     struct Student stu[3] = {  
  10.         { {1, 2, 3 }, 10 },  
  11.         { {4, 5, 6 }, 11 },  
  12.         { {7, 8, 9 }, 12 }  
  13.     };  
  14.   
  15.     // 可以简写为:  
  16.     struct Student stu[3] = {  
  17.         { 1, 2, 3, 10 },  
  18.         { 4, 5, 6, 11 },  
  19.         { 7, 8, 9, 12 }  
  20.     };  
  21.   
  22.     return 0;  
  23. }  

 

结构体与指针

 

[html]  view plain copy print ?  
 
  1. struct Student stu;  
  2. struct Student *p = &stu;  
  3. p->age = 26;  



 

 

 

 

 

结构体数组与指针

栈数组:

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     int a;  
  4.     int b;  
  5. };  
  6.   
  7. int main() {  
  8.   
  9.     struct Student stu[3];  
  10.     struct Student *p = stu;  
  11.   
  12.     int i;  
  13.     for (i = 0; i < 3; i++) {  
  14.         p->a = 10 + i;  
  15.         p->b = 20 + i;  
  16.         p++;  
  17.     }  
  18.   
  19.     printf("%d,%d\n", stu[0].a, stu[0].b);  
  20.     printf("%d,%d\n", stu[1].a, stu[1].b);  
  21.     printf("%d,%d\n", stu[2].a, stu[2].b);  
  22.     return 0;  
  23. }  

运行结果:

C——结构体总结_数组

堆数组:

[html]  view plain copy print ?  
 
  1. struct Student  
  2. {  
  3.     int a;  
  4.     int b;  
  5. };  
  6.   
  7. int main() {  
  8.   
  9.     struct Student *p;  
  10.     p = malloc(sizeof(struct Student) * 3);  
  11.     struct Student *arr = p;//arr指针记录了p的初始指针  
  12.     p->a = 1;  
  13.     p->b = 2;  
  14.     p++;// 导致p指针的位置发生偏移,  
  15.     p->a = 3;  
  16.     p->b = 4;  
  17.       
  18.     printf("%d,%d\n", arr[0].a, arr[0].b);  
  19.     printf("%d,%d\n", arr[1].a, arr[1].b);  
  20.   
  21.     //free(p);// 因为p的指针发生偏移,free(p)会发生异常  
  22.     free(arr);// 可以正常释放malloc申请的堆内存  
  23.     return 0;  
  24. }  

运行结果:

C——结构体总结_初始化_02

 

结构体成员内存对齐图解

C——结构体总结_html_03

 

结构体嵌套对齐图解:

C——结构体总结_结构体数组_04