構(gòu)體與共用體(二))
第十章 結(jié)構(gòu)體與共用體第二節(jié) 結(jié)構(gòu)體數(shù)組,結(jié)構(gòu)體指針1. 結(jié)構(gòu)體數(shù)組單獨寫成一行來定義struct student stu[3];定義結(jié)構(gòu)的時候順便定義結(jié)構(gòu)體數(shù)組structstudent{intnum;//學(xué)號charname[100];//姓名intsex;//性別,女:0, 男:1intage;//年齡charaddress[100];//地址structdatebirthday;//生日,結(jié)構(gòu)體類型變量}stu[3];定義結(jié)構(gòu)體數(shù)組的時候可以同時進行初始化#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//學(xué)號charname[100];//姓名intsex;//性別,女:0, 男:1intage;//年齡charaddress[100];//地址structdatebirthday;//生日,結(jié)構(gòu)體類型變量};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. 結(jié)構(gòu)體指針就是結(jié)構(gòu)體變量的指針這個指針肯定是指向該結(jié)構(gòu)體變量所占據(jù)的內(nèi)存的起始地址結(jié)構(gòu)體變量指針也可以指向結(jié)構(gòu)體數(shù)組的元素因為結(jié)構(gòu)體數(shù)組中的每個元素就相當于一個結(jié)構(gòu)體變量如何通過結(jié)構(gòu)體變量的指針來訪問結(jié)構(gòu)體變量的成員呢?(*pstu).age, 第一種使用(*pstu),因為*優(yōu)先級不如結(jié)構(gòu)體成員運算符.高,所以這里要添加括號pstu-age, 第二種使用pstu-, 其中這個-叫指向結(jié)構(gòu)體成員運算符,優(yōu)先級非常高#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//學(xué)號charname[100];//姓名intsex;//性別,女:0, 男:1intage;//年齡charaddress[100];//地址structdatebirthday;//生日,結(jié)構(gòu)體類型變量};intmain(intargc,char*argv[]){structstudentstu;//定義一個結(jié)構(gòu)體變量structstudent*pstu;//定義一個結(jié)構(gòu)體變量的指針//現(xiàn)在,pstu就指向了stu,stu里邊的內(nèi)容改變了,就等于pstu所指向的內(nèi)容改變了pstustu;strcpy(stu.name,Felaim);stu.age18;// *pstu stu(*pstu).num100;printf(name: %s\n,pstu-name);printf(age: %d\n,stu.age);return0;}指向結(jié)構(gòu)體數(shù)組的指針#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//學(xué)號charname[100];//姓名intsex;//性別,女:0, 男:1intage;//年齡charaddress[100];//地址structdatebirthday;//生日,結(jié)構(gòu)體類型變量};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;//數(shù)組名代表的是數(shù)組首地址,數(shù)組中數(shù)據(jù)在內(nèi)存中都是挨著的inti;for(i0;i3;i){printf(name: %s\n,pstu-name);printf(num: %d\n,pstu-num);pstu;}// 一個結(jié)構(gòu)體有224個字節(jié)printf(sizeof struct: %d bytes\n,sizeof(structstudent));return0;}說明printf(“%d\n”, (pstu)-num);//先加后用, 101printf(“%d\n”, (pstu)-num);//先用后加, 100pstu stu[1];//不能讓pstu指向數(shù)組元素中的成員(pstu stu[1].name)3. 用指向結(jié)構(gòu)體的指針做函數(shù)參數(shù)#includeiostream#includecstringstructdate{intyear;intmonth;intday;};structstudent{intnum;//學(xué)號charname[100];//姓名intsex;//性別,女:0, 男:1intage;//年齡charaddress[100];//地址structdatebirthday;//生日,結(jié)構(gòu)體類型變量};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;//學(xué)號charname[100];//姓名intsex;//性別,女:0, 男:1intage;//年齡charaddress[100];//地址structdatebirthday;//生日,結(jié)構(gòu)體類型變量};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);// 發(fā)現(xiàn)并沒有改變,因為是值傳遞,改變的形參的內(nèi)存,并沒有改變實參的內(nèi)存,這是兩塊內(nèi)存// 將完整的結(jié)構(gòu)體變量直接傳給func中,相當于做了內(nèi)存內(nèi)容的拷貝,開銷很大,費時費空間func2(stu[0]);printf(address: %s\n,stu[0].address);return0;}建議函數(shù)參數(shù)盡量使用地址和指針