一、Xmind整理:
二、课上练习:
练习1:定义车的信息:品牌,单价,颜色,车牌号
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}a;struct Car//结构体自定义类型struct Car a;struct Car arr[3]struct Car *p;
练习2:间接定义变量按顺序初始化
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car={"大众",123456,"白色"};car.name
练习3: 间接定义变量不按顺序初始化
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car={.price=123456,.color="白色",.id="沪A1111"};
练习4: 间接定义变量,单个赋值
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car;strcpy(car.name,"大众");car.price=123456;strcpy(car.color,"白色");strcpy(car.id,"沪A88888");
练习5: 间接定义变量,输入赋值
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car;scanf("%s",car.name);scanf("%d",&car.price);scanf("%s",car.color);scanf("%s",car.id);
练习6:直接定义变量按顺序初始化
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}car={"大众",123456,"白色","沪A1111"};
练习7:直接定义变量不按顺序初始化
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}car={.price=123456,.color="白色",.id="沪A1111",.name="大众"};
练习8:直接定义变量,单个赋值
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}car;strcpy(car.name,"大众");car.price=123456;strcpy(car.color,"白色");strcpy(car.id,"沪A88888");
练习9:直接定义变量,输入赋值
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}car;scanf("%s",car.name);scanf("%d",&car.price);scanf("%s",car.color);scanf("%s",car.id);
练习10:只有直接定义可以省略结构体名,无名结构体
struct {char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}car;
练习11:间接定义变量按顺序初始化
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car[3]={"大众",123456,"白色","沪A1111","大众",123456,"白色","沪A2222","大众",123456,"白色","沪A33333"};
练习12:间接定义变量不按顺序初始化
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car[3]={[0]={.price=123456,.color="白色",.id="沪A1111",.name="大众"},[2]={.price=123456,.color="白色",.id="沪A2222",.name="大众"},[1]={.price=123456,.color="白色",.id="沪A3333",.name="大众"}};
练习13: 间接定义变量,单个赋值
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car[3];car[0]:第一辆车的所有信息car[0].name:第一辆车的品牌car[2].idstrcpy(car[0].name,"大众");car[0].price=123456;strcpy(car[0].color,"白色");strcpy(car[0].id,"沪A88888");strcpy(car[1].name,"大众");car[1].price=123456;strcpy(car[1].color,"白色");strcpy(car[1].id,"沪A88888");strcpy(car[2].name,"大众");car[2].price=123456;strcpy(car[2].color,"白色");strcpy(car[2].id,"沪A88888");
练习14: 间接定义变量,输入赋值
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car[3];for(int i=0;i<3;i++){scanf("%s",car[i].name);scanf("%d",&car[i].price);scanf("%s",car[i].color);scanf("%s",car[i].id);}
练习15:直接定义变量按顺序初始化
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号} car[3]={"大众",123456,"白色","沪A1111","大众",123456,"白色","沪A2222","大众",123456,"白色","沪A33333"};
练习16:直接定义变量不按顺序初始化
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}car[3]={[0]={.price=123456,.color="白色",.id="沪A1111",.name="大众"},[2]={.price=123456,.color="白色",.id="沪A2222",.name="大众"},[1]={.price=123456,.color="白色",.id="沪A3333",.name="大众"}};
练习17:直接定义变量,单个赋值
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}car[3];car[0]:第一辆车的所有信息car[0].name:第一辆车的品牌car[2].idstrcpy(car[0].name,"大众");car[0].price=123456;strcpy(car[0].color,"白色");strcpy(car[0].id,"沪A88888");strcpy(car[1].name,"大众");car[1].price=123456;strcpy(car[1].color,"白色");strcpy(car[1].id,"沪A88888");strcpy(car[2].name,"大众");car[2].price=123456;strcpy(car[2].color,"白色");strcpy(car[2].id,"沪A88888");
练习18: 直接定义变量,输入赋值
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号}car[3];for(int i=0;i<3;i++){scanf("%s",car[i].name);scanf("%d",&car[i].price);scanf("%s",car[i].color);scanf("%s",car[i].id);}
练习19:结构体指针指向普通结构体变量的地址
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car c={.price=123456,.color="白色",.id="沪A1111",.name="大众"};struct Car *p=&c;
练习20: 结构体指针指向结构体数组的地址
struct Car{char name[20]; //品牌int price; //单价char color[10] //颜色char id[20] //车牌号};struct Car car[3]={"大众",123456,"白色","沪A1111","大众",123456,"白色","沪A2222","大众",123456,"白色","沪A33333"};struct Car *p=car;
练习21:结构体指针指向堆区空间的首地址
#include <stdio.h>
#include <string.h>
#include <stdlib.h>struct Car{char name[20]; //品牌int price; //单价char color[10]; //颜色char id[20] ; //车牌号};
//输出对去空间内存
void Output(struct Car *p,int len)
{for(int i=0;i<len;i++){// printf("%s\t%d\t%s\t%s\n",(p+i)->name,(p+i)->price,(p+i)->color,(p+i)->id);printf("%s\t%d\t%s\t%s\n",(*(p+i)).name,(*(p+i)).price,(*(p+i)).color,(*(p+i)).id);}
}
//在堆区空间申请
struct Car * create()
{struct Car *p=(struct Car *)malloc(sizeof(struct Car)*3);//if(NULL == p){return NULL;}return p;}
//在堆区实现循环输入
void Input(struct Car *p ,int n)
{for(int i=0;i<3;i++){scanf("%s",(p+i)->name);scanf("%d",&(p+i)->price);//(p+i)->price===>pricescanf("%s",(p+i)->color);scanf("%s",(p+i)->id);}}
//释放堆区空间
struct Car * free_space(struct Car *p)
{if(NULL==p){return NULL;}free(p);
p=NULL;return p;
}
int main(int argc, const char *argv[])
{/*struct Car car[3]={"大众",123456,"白色","沪A1111","大众",123456,"白色","沪A2222","大众",123456,"白色","沪A33333"};struct Car *p=car;Output(p,3);*/struct Car *p=create();Input(p,3);Output(p,3);p=free_space(p);return 0;
}
练习22:结构体的引用
struct Car{char name[20]; //品牌int price; //单价char color[10]; //颜色char id[20] ; //车牌号};struct Car a={"大众",123456,"白色","沪A1111"};struct Car b;//把a赋值给bstrcpy(b.name,a.name);b.price=a.price;strcpy(b.color,a.color);strcpy(b.id,a.id);a:表示所有信息b=a;
练习23:typedef和结构体结合
//typedef int a,arr[],p;//typedef int arr[];typedef struct {char name[20]; //品牌int price; //单价char color[10]; //颜色char id[20] ; //车牌号}car_t,car_arr[3],*car_p ;//a==》car_p
car_t:不是变量名,是普通结构体类型别名
car_arr:不是结构体数组名,是结构体数组的别名
car_p:不是结构体指针变量名,是结构体指针的别名
当typedef和结构体结合时,结构体名可以省略不写
练习24:结构体嵌套普通结构
定义一个学生:姓名、性别、出生日期【年月日】
typedef struct{int year,month,day;}Birthday;typedef struct{char name[10];char sex;Birthday bir;}Stu;Stu student={"张三",'M',2000,7,14};printf("%s %c %d年%d月%d日\n",student.name,studnet.sex,student.bir.year,student.bir.month,student.bir.day);
练习25:结构体嵌套结构体数组
定义一个人的信息:姓名,性别,年龄,车辆多辆【品牌,单价】
typedef struct{char name[10];int price; }Car_t;typedef struct{char name[10];char sex;int age;Car_t arr[2]; }person;person per={"张三",'M',18,"奔驰",555555,"宝马",666666};printf("",per.name,per.sex,per.age,per.arr[0].name,per.arr[0].price,per.arr[1].name,per.arr[1].price);
练习26:结构体字节计算
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{typedef struct{int a;float b;}A_t;A_t var;printf("&var=%p\t&var.a=%p\t&var.b=%p\n",&var,&var.a,&var.b);printf("sizeof(A_t)=%ld\n",sizeof(A_t));//8typedef struct{char a;double b;}B_t;
B_t var1;printf("&var1=%p\t&var1.a=%p\t&var1.b=%p\n",&var1,&var1.a,&var1.b);printf("sizeof(B_t)=%ld\n",sizeof(B_t));//16typedef struct{int a;char b;float c;char *p;char d;}C_t;
C_t c;printf("c.a=%p\n",&c.a);printf("c.b=%p\n",&c.b);printf("c.c=%p\n",&c.c);printf("c.p=%p\n",&c.p);printf("c.d=%p\n",&c.d);printf("sizeof(C_t)=%ld\n",sizeof(C_t));//32typedef char a_str[5]; typedef struct{a_str a;int b;}D_t;printf("sizeof(D_t)=%ld\n",sizeof(D_t));//12typedef struct{char a;double b;}E_t;typedef struct {char a;short b;E_t c;}F_t;printf("sizeof(F_t)=%ld\n",sizeof(F_t));//24return 0;
}
练习27:共用体:默认为第一个元素赋值
typedef union {int a;char b;double c;}A_t;
A_t var={97};//默认为第一个成员赋值
练习28:指定某一个成员赋值
typedef union {int a;char b;double c;}A_t;
A_t var1={.c=55.66};//指定为某一成员赋值
练习29:共用体和结构体结合【共用体在结构体外】
typedef union{int a;double b; }A_t;typedef struct{int a_v;A_t var; }B_t;B_t a={100,20};//默认为共用体第一个成员a赋值B_t b={100,.var.b=55.66};//指定共用体成员b赋值a.a_v=100a.var.a=20b.a_v=100b.var.b=55.66
练习30: 共用体和结构体结合 【共用体在结构体内】不建议写共用体名
typedef struct{int a_v;union {int a;double b; };}B_t;B_t b={100,.b=55.66};