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,
};
#include <stdio.h>#include <stdlib.h>typedef struct{int name;int id;char *addr;char str[10];int hight;}NAME;int main(){NAME name2={name:11,.id=21,addr:"case"};strcpy(name2.str,"case1");//对结构体中的字符串赋值name2.hight=33;printf("name2.name:%d\n",name2.name);printf("name2.id:%d\n",name2.id);printf("name2.addr:%s\n",name2.addr);printf("name2.addr:%s\n",name2.str);printf("name2.addr:%d\n",name2.hight);}//结果name2.name:11name2.id:21name2.addr:casename2.str:case1name2.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最大的放上面。但不利于阅读
结构体偏移量
struct Person p{char a;int b;};取b*((int*)&p+1) 或*(int*) ((char*)&p + sizeof(int))//(char*)&p p的地址类型转为字符;((char*)&p + sizeof(int)) p地址移位的 步长(int*) 从给定首地址开始取 int类型大小字节数
5、结构体和指针
成员变量是指针:
若指针指向堆空间。那么如果发生赋值行为,就会产生两个问题:
1、同一块空间被释放两次;
2、内存泄漏。
结构体指针 变量;
结构体指针
//结构体中有指针变量;struct stuinfo{char* name; //指针int age;};struct stuinfo si;si.name=(char *)malloc(sizeof(char)*21); //开辟指针堆空间strcpy(si.name,"张三);si.age = 18;printf("%s %d\n",si.name,si.age);free(si.name); //释放堆空间********************************对照区别*********************************************//结构体指针struct stuinfo* sp = &si;sp->name = (char*)malloc(sizeof(char)*21;strcpy(sp->name,"李");sp->age = 18;free(sp->name);//堆空间创建 结构体指针变量struct stuinfo* sp = (struct stuinfo*)malloc(sizeof(stu));//堆空间 结构体sp->name = malloc(21);free(sp->name);free(sp);
堆空间 实现 冒泡
struct student {char* name;float* scores;};//为什么*3?因为创建3个学生的堆空间struct student* p= (struct student *)malloc(sizeof(struct student)*3);//堆空间 连续的内存空间,都能使用下标的方式访问内存for(int i=0; i<3; i++){p[i].name= (char*)malloc(sizeof(char)*21);p[i].scores= (float*)malloc(sizeof(char)*3); //堆空间存3门成绩printf("请输入姓名 3门成绩\n");scanf("%s%f%f%f",p[i].name,&p[i].scores[0],&p[i].scores[1],&p[i].scores[2]);}//冒泡排序for(int i=0;i<3-1;i++){for(int j=0;j<3-1-i;j++){int sum1 = p[j].scores[0]+p[j].scores[1]+p[j].scores[2];int sum2 = p[j+1].scores[0]+p[j+1].scores[1]+p[j+1].scores[2];if(sum1>sum2){struct student2 temp = p[j];p[j] = p[j+1];p[j+1] = temp;}}}//打印for(int j =0;j<3;j++){printf("%s,%f,%f,%f\n", p[i].name, p[i].scores[0], p[i]scores[1], p[i].scores[2]);}//释放堆空间for(int i=0;i<3;i++){free(p[i].name);free(p[i].scores);}free(p);
6、结构体数组
struct students{char name[21];unsigned int age;char tel[16];float scores[3];char sex;};//结构体数组struct students stu[2];for(int i=0; i<2; i++){printf("请输入姓名 年龄 电话 3门成绩,性别\n");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);}for(int i=0; i<2; i++){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);}
结构体数组 实现 冒泡
//结构体排序struct student {char name[20];float scores[3];};struct student stu3[2];for(int i=0; i<2; i++){printf("请输入姓名 3门成绩\n");scanf("%s%f%f%f", stu3[i].name,&stu3[i].scores[0],&stu3[i].scores[1],&stu3[i].scores[2]);}for(int i =0; i<2-1;i++){for(int j=0;j<2-1-i;j++){/******int sum1 = stu3[j].scores[0]+stu3[j].scores[1]+stu3[j].scores[2];int sum2 = stu3[j+1].scores[0]+stu3[j+1].scores[1]+stu3[j+1].scores[2];if(sum1>sum2){//交换姓名char temp[20];strcpy(temp, stu3[j].name);strcpy(stu3[j].name, stu3[j+1].name);strcpy(stu3[j+1].name, temp);//交换成绩for(int k =0;k<3;k++){float temp = stu3[j].scores[k];stu3[j].scores[k] = stu3[j+1].scores[k];stu3[j+1].scores[k] = temp;*******///结构体变量 优化;struct student temp = s[j];s[j] = s[j+1];s[j+1] = temp;}}}}for(int i=0; i<2; i++){printf("%s,%.1f,%.1f,%.1f\n",stu3[i].name,stu3[i].scores[0],stu3[i].scores[1],stu3[i].scores[2]);}
结构体指针数组
元素都是结构体指针
结构体数组作为 函数参
函数参数中有指针、数组,都会转化为指针
数组作为函数参数,退化为指向首元素地址的指针
struct student {char name[20];float scores[3];};struct student stu3[2]; //结构体数组//参数是结构体数组 转化为指针,是指向struct student 类型的首元素的地址,即struct student*void func(struct student* stu3){}
7、结构体 做 函数参数
结构体 值传递 fun1(struct student s);
结构体指针 地址传递 fun2(struct student* s);
函数返回值是结构体,可以用结构体类型的变量接收
函数返回值是结构体地址,可以用结构体指针类型的变量接收
void BubbleSort(struct student2* p, int len){for(int i=0;i<3-1;i++){for(int j=0;j<3-1-i;j++){int sum1 = p[j].scores[0]+p[j].scores[1]+p[j].scores[2];int sum2 = p[j+1].scores[0]+p[j+1].scores[1]+p[j+1].scores[2];if(sum1>sum2){struct student2 temp = p[j];p[j] = p[j+1];p[j+1] = temp;}}}}int main(){// 堆空间 结构体指针数struct student2* p= (struct student2 *)malloc(sizeof(struct student2)*3);//堆空间 连续的内存空间,都能使用下标的方式访问内存for(int i=0; i<3; i++){p[i].name= (char*)malloc(sizeof(char)*21);p[i].scores= (float*)malloc(sizeof(char)*3); //堆空间存3门成绩printf("请输入姓名 3门成绩\n");scanf("%s%f%f%f",p[i].name,&p[i].scores[0],&p[i].scores[1],&p[i].scores[2]);}BubbleSort(p, 3);for(int j =0;j<3;j++){printf("%s,%f,%f,%f\n", p[j].name, p[j].scores[0], p[j].scores[1], p[j].scores[2]);}//释放堆空间for(int i=0;i<3;i++){free(p[i].name);free(p[i].scores);}free(p);}
8、结构体嵌套
struct stra{int a; 4float b; 4char c; 1};struct strb{double d; 8->8char* e; 4->4short f; 2->4struct stra abc; 4+4+1->8=16};int main(){struct strb strbb;strbb.d = 12;strbb.stra.a = 10;struct strb strbbb = {4, &e, f, 1, 2.2, a}printf("%d\n",strbb.stra.a);}
