ARTICLE DETAIL

资讯详情

深耕商务建站与企业官网运营的一线实战洞察。

【c++】c语言入门到c++使用高手: c语言入门之结构体与共用体(二)

【c++】c语言入门到c++使用高手: c语言入门之结构体与共用体(二) 第十章 结构体与共用体第二节 结构体数组,结构体指针1. 结构体数组单独写成一行来定义struct student stu[3];定义结构的时候顺便定义结构体数组structstudent{intnum;//学号charname[100];//姓名intsex;//性别,女:0, 男:1intage;//年龄charaddress[100];//地址structdatebirthday;//生日,结构体类型变量}stu[3];定义结构体数组的时候可以同时进行初始化#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//学号charname[100];//姓名intsex;//性别,女:0, 男:1intage;//年龄charaddress[100];//地址structdatebirthday;//生日,结构体类型变量};intmain(intargc,char*argv[]){structstudentstu[3]{{100,Felaim,0,18,aa,{2002,11,15}},{101,Zhang,1,19,bb,{2001,7,8}},{99,Wang,1,17,cc,{2003,12,12}}};stu[1].age;//20printf(name: %s\n age: %d\n,stu[1].name,stu[1].age);return0;}2. 结构体指针就是结构体变量的指针这个指针肯定是指向该结构体变量所占据的内存的起始地址结构体变量指针也可以指向结构体数组的元素因为结构体数组中的每个元素就相当于一个结构体变量如何通过结构体变量的指针来访问结构体变量的成员呢?(*pstu).age, 第一种使用(*pstu),因为*优先级不如结构体成员运算符.高,所以这里要添加括号pstu-age, 第二种使用pstu-, 其中这个-叫指向结构体成员运算符,优先级非常高#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//学号charname[100];//姓名intsex;//性别,女:0, 男:1intage;//年龄charaddress[100];//地址structdatebirthday;//生日,结构体类型变量};intmain(intargc,char*argv[]){structstudentstu;//定义一个结构体变量structstudent*pstu;//定义一个结构体变量的指针//现在,pstu就指向了stu,stu里边的内容改变了,就等于pstu所指向的内容改变了pstustu;strcpy(stu.name,Felaim);stu.age18;// *pstu stu(*pstu).num100;printf(name: %s\n,pstu-name);printf(age: %d\n,stu.age);return0;}指向结构体数组的指针#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//学号charname[100];//姓名intsex;//性别,女:0, 男:1intage;//年龄charaddress[100];//地址structdatebirthday;//生日,结构体类型变量};intmain(intargc,char*argv[]){structstudentstu[3]{{100,Felaim,0,18,aa,{2002,11,15}},{101,Zhang,1,19,bb,{2001,7,8}},{99,Wang,1,17,cc,{2003,12,12}}};structstudent*pstu;pstustu;//数组名代表的是数组首地址,数组中数据在内存中都是挨着的inti;for(i0;i3;i){printf(name: %s\n,pstu-name);printf(num: %d\n,pstu-num);pstu;}// 一个结构体有224个字节printf(sizeof struct: %d bytes\n,sizeof(structstudent));return0;}说明printf(“%d\n”, (pstu)-num);//先加后用, 101printf(“%d\n”, (pstu)-num);//先用后加, 100pstu stu[1];//不能让pstu指向数组元素中的成员(pstu stu[1].name)3. 用指向结构体的指针做函数参数#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//学号charname[100];//姓名intsex;//性别,女:0, 男:1intage;//年龄charaddress[100];//地址structdatebirthday;//生日,结构体类型变量};voidfunc1(structstudent*pd){pd-age120;}intmain(intargc,char*argv[]){structstudentstu[3]{{100,Felaim,0,18,aa,{2002,11,15}},{101,Zhang,1,19,bb,{2001,7,8}},{99,Wang,1,17,cc,{2003,12,12}}};structstudent*pstu;pstustu;func1(pstu);printf(age: %d\n,(pstu)-age);printf(age: %d\n,stu[0].age);return0;}实例#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//学号charname[100];//姓名intsex;//性别,女:0, 男:1intage;//年龄charaddress[100];//地址structdatebirthday;//生日,结构体类型变量};voidfunc1(structstudent*pd){pd-age120;}voidfunc2(structstudentpd){strcpy(pd.address,123);}intmain(intargc,char*argv[]){structstudentstu[3]{{100,Felaim,0,18,aa,{2002,11,15}},{101,Zhang,1,19,bb,{2001,7,8}},{99,Wang,1,17,cc,{2003,12,12}}};structstudent*pstu;pstustu;func1(pstu);printf(age: %d\n,(pstu)-age);printf(age: %d\n,stu[0].age);// 发现并没有改变,因为是值传递,改变的形参的内存,并没有改变实参的内存,这是两块内存// 将完整的结构体变量直接传给func中,相当于做了内存内容的拷贝,开销很大,费时费空间func2(stu[0]);printf(address: %s\n,stu[0].address);return0;}建议函数参数尽量使用地址和指针
返回列表
PREV
查看更多资讯
NEXT
返回资讯列表