1、结构体

用户自定义的可用的数据类型,可以存储不同类型的数据项;
定义:
struct tags{
numberlist1;
numberlist2;
}variable-list;
tags:结构体标签;
numberlist是标准的变量定义;如 int 1;结构体成员可以包含其他结构体,也可以包含指向自己结 构体类型的指针;
variable-list:结构变量,可以指定多个;

2、定义结构体:

三种方式->1、定义结构体,同时定义结构体变量;
struct {
int a;
char b;
double c;
void (fun)(void); //结构体成员指向函数的指针;
}s1;
->2、定义结构体;
struct SIMPLE{
int a;
char b;
double c;
};
另外声明了变量t1、t2、t3
struct SIMPLE t1,t2[20],
t3;
->3、使用typedef创建新类型;
typedef struct SIMPLE{
int a;
char[20];
doublec;
}simple;
用新类型声明新的结构体变量;
simple s1; //struct SIMPLE等价于此处的 simple

初始化 结构体成员 :

只能在定义变量的同时进行初始化;
struct SIMPLE{
int a;
char b;
float c;
}s1;
1、可以定义时赋值,如:
struct SIMPLE s1={1,”ada”,0.25}; //需要注意赋值时的顺序要对应。不能错位。
2、可以定义后逐个赋值,如:
struct SIMPLE s1;
s1.a=1;s1.”ada”;s1.c=0.25;
3、可以定义时乱序赋值
(C风格):
struct SIMPLE s1{
.a=1,
.b=”ada”,
.c=0.25,
};
(C++风格)类似键值对:
struct SIMPLE s1{
a:1,
b:”ada”,
c:0.25,
};

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct{
  4. int name;
  5. int id;
  6. char *addr;
  7. char str[10];
  8. int hight;
  9. }NAME;
  10. int main(){
  11. NAME name2={
  12. name:11,
  13. .id=21,
  14. addr:"case"
  15. };
  16. strcpy(name2.str,"case1");//对结构体中的字符串赋值
  17. name2.hight=33;
  18. printf("name2.name:%d\n",name2.name);
  19. printf("name2.id:%d\n",name2.id);
  20. printf("name2.addr:%s\n",name2.addr);
  21. printf("name2.addr:%s\n",name2.str);
  22. printf("name2.addr:%d\n",name2.hight);
  23. }
  24. //结果
  25. name2.name:11
  26. name2.id:21
  27. name2.addr:case
  28. name2.str:case1
  29. name2.hight:33

结构体自引用:使用指针
三种方式->1、
typedef struct FSMSTATE{
int a,
char b,
struct FSMSTATE *nextstate, //结构体自引用
}FSMState;
三种方式->2、
struct FSM_STATE
;
typedef struct FSMSTATE FSMState; //先定义,下面再用
struct FSMSTATE{
int value,
FSMState next_state,
};
结构体*相互引用
:

3、访问 结构体成员:

成员访问运算符.如:tag.numberlist1;
如果结构中的成员是常规变量(包括结构变量),则使用“.”;如果成员变量是指针(无论它们指向什么,包括结构变量),则使用“->”。
作为函数参数;如:void fun1(struct tags tag);
指向结构的指针:访问必须使用->运算符;
struct Books* struct_pointer = &Book1; //Book1是结构体;
struct_pointer->title = **;

4、结构体大小和内存存储结构

大小: 并不是成员变量的sizeof()之和;而是最大成员变量的sizeof 的最小倍数的 和;
可以节省空间的方法:sizeof最大的放上面。但不利于阅读

结构体偏移量

  1. struct Person p{
  2. char a;
  3. int b;
  4. };
  5. 取b
  6. *((int*)&p+1) 或
  7. *(int*) ((char*)&p + sizeof(int))
  8. //(char*)&p p的地址类型转为字符;
  9. ((char*)&p + sizeof(int)) p地址移位的 步长
  10. (int*) 从给定首地址开始取 int类型大小字节数

5、结构体和指针

成员变量是指针:
若指针指向堆空间。那么如果发生赋值行为,就会产生两个问题:
1、同一块空间被释放两次;
2、内存泄漏。
结构体指针 变量;

结构体指针

  1. //结构体中有指针变量;
  2. struct stuinfo{
  3. char* name; //指针
  4. int age;
  5. };
  6. struct stuinfo si;
  7. si.name=(char *)malloc(sizeof(char)*21); //开辟指针堆空间
  8. strcpy(si.name,"张三);
  9. si.age = 18;
  10. printf("%s %d\n",si.name,si.age);
  11. free(si.name); //释放堆空间
  12. ********************************对照区别*********************************************
  13. //结构体指针
  14. struct stuinfo* sp = &si;
  15. sp->name = (char*)malloc(sizeof(char)*21;
  16. strcpy(sp->name,"李");
  17. sp->age = 18;
  18. free(sp->name);
  19. //堆空间创建 结构体指针变量
  20. struct stuinfo* sp = (struct stuinfo*)malloc(sizeof(stu));
  21. //堆空间 结构体
  22. sp->name = malloc(21);
  23. free(sp->name);
  24. free(sp);

堆空间 实现 冒泡

  1. struct student {
  2. char* name;
  3. float* scores;
  4. };
  5. //为什么*3?因为创建3个学生的堆空间
  6. struct student* p= (struct student *)malloc(sizeof(struct student)*3);
  7. //堆空间 连续的内存空间,都能使用下标的方式访问内存
  8. for(int i=0; i<3; i++){
  9. p[i].name= (char*)malloc(sizeof(char)*21);
  10. p[i].scores= (float*)malloc(sizeof(char)*3); //堆空间存3门成绩
  11. printf("请输入姓名 3门成绩\n");
  12. scanf("%s%f%f%f",p[i].name,&p[i].scores[0],&p[i].scores[1],&p[i].scores[2]);
  13. }
  14. //冒泡排序
  15. for(int i=0;i<3-1;i++){
  16. for(int j=0;j<3-1-i;j++){
  17. int sum1 = p[j].scores[0]+p[j].scores[1]+p[j].scores[2];
  18. int sum2 = p[j+1].scores[0]+p[j+1].scores[1]+p[j+1].scores[2];
  19. if(sum1>sum2){
  20. struct student2 temp = p[j];
  21. p[j] = p[j+1];
  22. p[j+1] = temp;
  23. }
  24. }
  25. }
  26. //打印
  27. for(int j =0;j<3;j++){
  28. printf("%s,%f,%f,%f\n", p[i].name, p[i].scores[0], p[i]scores[1], p[i].scores[2]);
  29. }
  30. //释放堆空间
  31. for(int i=0;i<3;i++){
  32. free(p[i].name);
  33. free(p[i].scores);
  34. }
  35. free(p);

6、结构体数组

  1. struct students{
  2. char name[21];
  3. unsigned int age;
  4. char tel[16];
  5. float scores[3];
  6. char sex;
  7. };
  8. //结构体数组
  9. struct students stu[2];
  10. for(int i=0; i<2; i++){
  11. printf("请输入姓名 年龄 电话 3门成绩,性别\n");
  12. scanf("%s%d%s%f%f%f,%c",stu[i].name,&stu[i].age,stu[i].tel,&stu[i].scores[0],&stu[i].scores[1],&stu[i].scores[2],&stu[i].sex);
  13. }
  14. for(int i=0; i<2; i++){
  15. printf("%s, %d, %s, %.1f,%.1f,%.1f, %c\n",stu[i].name,stu[i].age,stu[i].tel,stu[i].scores[0],stu[i].scores[1],stu[i].scores[2],stu[i].sex);
  16. }

结构体数组 实现 冒泡

  1. //结构体排序
  2. struct student {
  3. char name[20];
  4. float scores[3];
  5. };
  6. struct student stu3[2];
  7. for(int i=0; i<2; i++){
  8. printf("请输入姓名 3门成绩\n");
  9. scanf("%s%f%f%f", stu3[i].name,&stu3[i].scores[0],&stu3[i].scores[1],&stu3[i].scores[2]);
  10. }
  11. for(int i =0; i<2-1;i++){
  12. for(int j=0;j<2-1-i;j++){
  13. /******
  14. int sum1 = stu3[j].scores[0]+stu3[j].scores[1]+stu3[j].scores[2];
  15. int sum2 = stu3[j+1].scores[0]+stu3[j+1].scores[1]+stu3[j+1].scores[2];
  16. if(sum1>sum2){
  17. //交换姓名
  18. char temp[20];
  19. strcpy(temp, stu3[j].name);
  20. strcpy(stu3[j].name, stu3[j+1].name);
  21. strcpy(stu3[j+1].name, temp);
  22. //交换成绩
  23. for(int k =0;k<3;k++){
  24. float temp = stu3[j].scores[k];
  25. stu3[j].scores[k] = stu3[j+1].scores[k];
  26. stu3[j+1].scores[k] = temp;
  27. *******/
  28. //结构体变量 优化;
  29. struct student temp = s[j];
  30. s[j] = s[j+1];
  31. s[j+1] = temp;
  32. }
  33. }
  34. }
  35. }
  36. for(int i=0; i<2; i++){
  37. printf("%s,%.1f,%.1f,%.1f\n",stu3[i].name,stu3[i].scores[0],stu3[i].scores[1],stu3[i].scores[2]);
  38. }

结构体指针数组

元素都是结构体指针

结构体数组作为 函数参

函数参数中有指针、数组,都会转化为指针
数组作为函数参数,退化为指向首元素地址的指针

  1. struct student {
  2. char name[20];
  3. float scores[3];
  4. };
  5. struct student stu3[2]; //结构体数组
  6. //参数是结构体数组 转化为指针,是指向struct student 类型的首元素的地址,即struct student*
  7. void func(struct student* stu3){
  8. }

7、结构体 做 函数参数

结构体 值传递 fun1(struct student s);
结构体指针 地址传递 fun2(struct student* s);

函数返回值是结构体,可以用结构体类型的变量接收
函数返回值是结构体地址,可以用结构体指针类型的变量接收

  1. void BubbleSort(struct student2* p, int len){
  2. for(int i=0;i<3-1;i++){
  3. for(int j=0;j<3-1-i;j++){
  4. int sum1 = p[j].scores[0]+p[j].scores[1]+p[j].scores[2];
  5. int sum2 = p[j+1].scores[0]+p[j+1].scores[1]+p[j+1].scores[2];
  6. if(sum1>sum2){
  7. struct student2 temp = p[j];
  8. p[j] = p[j+1];
  9. p[j+1] = temp;
  10. }
  11. }
  12. }
  13. }
  14. int main(){
  15. // 堆空间 结构体指针数
  16. struct student2* p= (struct student2 *)malloc(sizeof(struct student2)*3);
  17. //堆空间 连续的内存空间,都能使用下标的方式访问内存
  18. for(int i=0; i<3; i++){
  19. p[i].name= (char*)malloc(sizeof(char)*21);
  20. p[i].scores= (float*)malloc(sizeof(char)*3); //堆空间存3门成绩
  21. printf("请输入姓名 3门成绩\n");
  22. scanf("%s%f%f%f",p[i].name,&p[i].scores[0],&p[i].scores[1],&p[i].scores[2]);
  23. }
  24. BubbleSort(p, 3);
  25. for(int j =0;j<3;j++){
  26. printf("%s,%f,%f,%f\n", p[j].name, p[j].scores[0], p[j].scores[1], p[j].scores[2]);
  27. }
  28. //释放堆空间
  29. for(int i=0;i<3;i++){
  30. free(p[i].name);
  31. free(p[i].scores);
  32. }
  33. free(p);
  34. }

8、结构体嵌套

  1. struct stra{
  2. int a; 4
  3. float b; 4
  4. char c; 1
  5. };
  6. struct strb{
  7. double d; 8->8
  8. char* e; 4->4
  9. short f; 2->4
  10. struct stra abc; 4+4+1->8=16
  11. };
  12. int main(){
  13. struct strb strbb;
  14. strbb.d = 12;
  15. strbb.stra.a = 10;
  16. struct strb strbbb = {4, &e, f, 1, 2.2, a}
  17. printf("%d\n",strbb.stra.a);
  18. }

结构体 嵌套一级指针