商丘公司做网站,濮阳市做网站公司,英德市住房和城乡建设局网站,一般网站建设需求有哪些方面文章目录 前言一、结构体是什么#xff1f;二、结构体的定义三、结构体的初始化四、结构体的嵌套五、结构体数组 1结构体数组的定义#xff1a;六、结构体指针 一、结构体是什么#xff1f;
我们知道一群类型相同的数据组合到一起是数组#xff0c;那一群不同类型的数据组… 文章目录 前言一、结构体是什么二、结构体的定义三、结构体的初始化四、结构体的嵌套五、结构体数组 1结构体数组的定义六、结构体指针 一、结构体是什么
我们知道一群类型相同的数据组合到一起是数组那一群不同类型的数据组合到一起就是结构体。
二、结构体的定义 #includestdio.h int main(){ struct student { int id; char name[100]; int age; }; //上面的我们就定义了一个student结构体其中元素有idnameage } 三、结构体的初始化
1.普通初始化 #includestdio.h int main(){ struct people { char name[100];//姓名 int age;//年龄 char sex;//性别 }; struct people person1 {高欢,50,M}; printf(%s\n, person1.name); printf(%d\n, person1.age); printf(%c\n, person1.sex); } 运行结果 2.指定成员变量初始化 #includestdio.h int main(){ struct people { char name[100];//姓名 int age;//年龄 char sex;//性别 }; struct person2 { .sex F, .age 20, .name 秦良玉 }; printf(%s\n, person2.name); printf(%d\n, person2.age); printf(%c\n, person2.sex); } 运行结果 四、结构体的嵌套
1.结构体里面套了一个结构体就是结构体中有结构体类型的元素。
2.里面的结构体需要定义在前面而外层的结构体定义在后面。
嵌套结构体的定义 //内层结构体 struct birthday { int year; int month; int day; }; //外层结构体 struct student { int id; char name[100]; int age; struct birthday date; }; 嵌套结构体的代码案例 #includestdio.h int main(){ //结构体的嵌套其实就是结构体中的元素为结构体 //里面的结构体需要先定义外层结构体需要后定义。 struct birthday { int year; int month; int day; }; struct student { int id; char name[200]; int age; //结构体元素 struct birthday date; }; //普通初始化 struct student s1 {1,李如松,20,2004,5,7}; printf(%d\n, s1.id); printf(%s\n, s1.name); printf(%d\n, s1.age); printf(%d\n, s1.date.year); printf(%d\n, s1.date.month); printf(%d\n, s1.date.day); //2.指定成员变量初始化 struct student s2 { .id 2, .name 斛律光, .age 18, .date.year 2006, .date.month 12, .date.day 10 }; printf(%d\n, s2.id); printf(%s\n, s2.name); printf(%d\n, s2.age); printf(%d\n, s2.date.year); printf(%d\n, s2.date.month); printf(%d\n, s2.date.day); } 运行结果 五、结构体数组
和变量一样多个同类型的变量组合在一起就形成了数组那多个同类型的结构体组合在一起也形成了结构体数组。
1结构体数组的定义 struct student { int id; char name[200]; int age; }; struct student s[5];//定义了一个长度为5的结构体数组 代码案例 #includestdio.h int main(){ //结构体数组本质上是一个数组只不过其数组中的元素为结构体 struct student { int id; char name[200]; int age; }; struct student s[10]; s[0].id 1; s[0].age 18; scanf(%s, s[0].name);//手动输入name的值。 printf(%d\n, s[0].id); printf(%s\n, s[0].name); printf(%d\n, s[0].age); } 运行结果 六、结构体指针
结构体指针与函数指针一样本质上是指针只不过该指针指向的变量为结构体类型 struct student { int id; char name[200]; int age; }; struct student s1 {1,结构体指针,100}; struct student *p s1;//结构体指针的定义 代码案例 #includestdio.h int main(){ //结构体指针本质是一个指针其指向的类型为结构体类型的变量 struct student { int id; char name[200]; int age; }; struct student s1 {1,结构体指针,100}; struct student *p s1; //结构体指针第一种输出结果体内容的方式 printf(%d\n, (*p).id); printf(%s\n, (*p).name); printf(%d\n, (*p).age); //结构体指针第二种输出结果体内容的方式 printf(%d\n, p-id); printf(%s\n, p-name); printf(%d\n, p-age); } 运行结果 文章转载自: http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.wjlnz.cn.gov.cn.wjlnz.cn http://www.morning.gzzxlp.com.gov.cn.gzzxlp.com http://www.morning.pmdlk.cn.gov.cn.pmdlk.cn http://www.morning.mjgxl.cn.gov.cn.mjgxl.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.rdnjc.cn.gov.cn.rdnjc.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.knpbr.cn.gov.cn.knpbr.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.ntgrn.cn.gov.cn.ntgrn.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.zffps.cn.gov.cn.zffps.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.qyhcm.cn.gov.cn.qyhcm.cn http://www.morning.tsmcc.cn.gov.cn.tsmcc.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.zrrgx.cn.gov.cn.zrrgx.cn http://www.morning.nlygm.cn.gov.cn.nlygm.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.zypnt.cn.gov.cn.zypnt.cn http://www.morning.rwhlf.cn.gov.cn.rwhlf.cn http://www.morning.nlryq.cn.gov.cn.nlryq.cn http://www.morning.clzly.cn.gov.cn.clzly.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.hylbz.cn.gov.cn.hylbz.cn http://www.morning.rywr.cn.gov.cn.rywr.cn http://www.morning.gbfzy.cn.gov.cn.gbfzy.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.mrttc.cn.gov.cn.mrttc.cn http://www.morning.jmdpp.cn.gov.cn.jmdpp.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.mytmx.cn.gov.cn.mytmx.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.mkygc.cn.gov.cn.mkygc.cn http://www.morning.jfbgn.cn.gov.cn.jfbgn.cn http://www.morning.mzjbz.cn.gov.cn.mzjbz.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.wmhqd.cn.gov.cn.wmhqd.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.hqmfn.cn.gov.cn.hqmfn.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.whnps.cn.gov.cn.whnps.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.nzqqd.cn.gov.cn.nzqqd.cn http://www.morning.yjfmj.cn.gov.cn.yjfmj.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.tzzfy.cn.gov.cn.tzzfy.cn http://www.morning.ygqhd.cn.gov.cn.ygqhd.cn http://www.morning.fhddr.cn.gov.cn.fhddr.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.symgk.cn.gov.cn.symgk.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn