编程杂谈

结构体变量之间的传递直接把结构体赋值给另一个结构体变量#include<stdio.h>structDate{intyear;intmonth;intday;}date={.year=2021,.month=1,.day=5};intmain(void){structDated;d=date;printf("d的值:%d-%d-%d",d.year,d.month,d.day);return0;}结构体作为函数参数传递把结构体变量作为函数的参数传递给函数#include<stdio.h>//定义结构体structDate{intyear;intmonth;intday;};//定义函数structDategetDate(structDatedate){//给结构体赋值date.year=2021;date.month=1;date.day=5;//返回结构体returndate;}intmain(void){structDatedate,d;d=getDate(date);printf("日期为:%d-%d-%d",d.year,d.month,d.day);return0;}结构体指针作为函数参数传递函数直接传入结构体会降低性能,传入结构体指针能提高程序效率#include<stdio.h>structDate{intyear;intmonth;intday;};//传入指针不需要返回值voidgetDate(structDate*date){//给结构体赋值date->year=2021;date->month=1;date->day=5;}intmain(void){structDatedate;getDate(&date);//传入地址printf("日期为:%d-%d-%d",date.year,date.month,date.day);return0;}动态申请结构体空间动态申请,存储在堆空间,用完记得释放#include<stdio.h>#include<stdlib.h>structDate{intyear;intmonth;intday;};//传入指针不需要返回值voidgetDate(structDate*date){//给结构体赋值date->year=2021;date->month=1;date->day=5;}intmain(void){structDate*date;//申请空间date=(structDate*)malloc(sizeof(structDate));getDate(date);//传入地址printf("日期为:%d-%d-%d",date->year,date->month,date->day);//释放空间free(date);return0;}

编程杂谈

结构体定义方式关键字:structstruct结构体名称{基本类型变量名;基本类型变量名;...};//注意这有分号structBoy{charname[20];chargender;floatage;};structBoy{charname[20];chargender;floatage;}boy;//这样定义不用初始化boy为全局变量structBoy{charname[20];chargender;floatage;}boy={"大数据男孩",//可以不指定但顺序要对.gender='A',.age=18.5,};//初始化并设置默认值初始化结构体//基本初始化struct结构体名称变量名;structBoyboy;//初始化全部值值的类型需要对应struct结构体名称变量名={内容,""}structBoyboy={"大数据男孩","A",18.2}//初始化部分值struct结构体名称变量名={.定义结构=值,...}structBoyboy={.name="大数据男孩",.gender="A",}结构体使用结构体赋值结构体变量名.值=变量;boy.name="大数据男孩";获取结构体的值变量=结构体变量名.值floatage=boy.age;结构体的内存占用原因:C语言的内存对齐结构体使用#include<stdio.h>//定义结构体structBoy{charname[20];chargender;floatage;};//注意这里有分号intmain(){structBoyboy;//初始化结构体printf("输入姓名:");scanf("%s",boy.name);//数组不用取址符printf("输入性别:");scanf("%s",&boy.gender);printf("输入年龄:");scanf("%f",&boy.age);printf("--------信息录入完毕---------\n\n");printf("基本信息:\n");printf("姓名:%s\n",boy.name);printf("性别:%s\n",&boy.gender);printf("年龄:%.2f\n",boy.age);return0;}结构体嵌套//日期结构体structDate{intyear;intmonth;intday;};//男孩结构体structBoy{charname[20];chargender;structDatedate;//嵌套的日期结构体}boy={.name="大数据男孩",.gender='A',.date={2020,12,15},};访问嵌套结构体#include<stdio.h>//日期结构体structDate{intyear;intmonth;intday;};//男孩结构体structBoy{charname[20];chargender;structDatedate;//嵌套的日期结构体}boy={.name="大数据男孩",.gender='A',.date={2020,12,15},};intmain(){printf("姓名:%s\n",boy.name);printf("性别:%c\n",boy.gender);//嵌套的结构体一层一层的访问printf("日期:%d-%d-%d",boy.date.year,boy.date.month,boy.date.day);return0;}结构体数组structDate{intyear;intmonth;intday;}date[2]={{2020,12,15},{2020,12,16},};访问结构体数组#include<stdio.h>structDate{intyear;intmonth;intday;}date[2]={{2020,12,15},{2020,12,16},};intmain(){//结构体数组//structDatedate[2]={//{2020,12,15},//{2020,12,16},//};//访问结构体数组的内容printf("date[0]:%d-%d-%d\n",date[0].year,date[0].month,date[0].day);printf("date[1]:%d-%d-%d\n",date[1].year,date[1].month,date[1].day);return0;}结构体指针结构体指针本质是指针,作用是指向一个结构体structDatedate={2020,12,15};//定义一个结构体structDate*pt;//定义结构体指针pt=&date;//指针指向结构体通过结构体指针访问结构体#include<stdio.h>structDate{intyear;intmonth;intday;};intmain(){//先定义该结构体structDatedate={2020,12,15};//定义该结构体指针structDate*pt;//结构体指针指向结构体pt=&date;//获取结构体值printf("year:%d",date.year);//结构体直接访问printf("month:%d",(*pt).month);//通过结构体指针访问,括号必须的(优先级问题)printf("day:%d\n",pt->day);//结构体指针箭头访问return0;}

2020-12-27 761 0