




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第8章結構體與共用體C語言程序設計四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第1頁。本章內容結構體(結構structure)類型共用體(聯合union)類型結構體變量、結構體數組、結構體指針向函數傳遞結構體用結構體指針實現動態數據結構鏈表的概念及操作原理四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第2頁。思考一個問題在程序里表示一個人(姓名、年齡、性別…),怎么表示?想表示多個人呢?如何用計算機程序實現下述表格的管理?表8-1某學校學生成績管理表學號姓名性別入學時間計算機原理英語數學音樂1令狐沖男1999908372822林平之男1999789288783岳靈珊女1999897298664任盈盈女1999789587905……6……四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第3頁。數組的解決方法int studentId[30];/*最多可以管理30個學生,每個學生的學號用數組的下標表示*/char studentName[30][10]; char studentSex[30][2];
int timeOfEnter[30];/*入學時間用int表示*/int scoreComputer[30];/*計算機原理課的成績*/int scoreEnglish[30];/*英語課的成績*/ int scoreMath[30]; /*數學課的成績*/int scoreMusic[30]; /*音樂課的成績*/四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第4頁。intstudentId[30]={1,2,3,4,5,6};char studentName[30][10]={{"令狐沖"},{"林平之"},{"岳靈珊"},{"任盈盈"}};char studentSex[30][2]={{"男"},{"男"},{"女"},{"女"}};int timeOfEnter[30]={1999,1999,1999,1999};int scoreComputer[30]={90,78,89,78};int scoreEnglish[30]={83,92,72,95};int scoreMath[30]={72,88,98,87};int scoreMusic[30]={82,78,66,90};
數組的解決方法四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第5頁。數據的內存管理方式90788978……83927295……72889887……82786690……1234……令狐沖林平之岳靈珊任盈盈……男男女女……1999199919991999……數組的解決方法四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第6頁。數據的內存管理方式90788978……83927295……72889887……82786690……1234……令狐沖林平之岳靈珊任盈盈……男男女女……1999199919991999……數組的解決方法分配內存不集中,尋址效率不高對數組進行賦初值時,容易發生錯位結構顯得比較零散,不容易管理四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第7頁。希望的內存分配圖
1令狐沖男1999908372822林平之男1999789288783岳靈珊女1999897298664任盈盈女199978958790四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第8頁。結構體的解決方法structSTUDENT
{
intstudentID;/*每個學生的序號*/
charstudentName[10];/*每個學生的姓名*/ charstudentSex[4];/*每個學生的性別*/ int timeOfEnter;/*每個學生的入學時間*/ int scoreComputer;/*每個學生的計算機原理成績*/ int scoreEnglish;/*每個學生的英語成績*/ int scoreMath; /*每個學生的數學成績*/ int scoreMusic;/*每個學生的音樂成績*/
};struct
STUDENT是一個類型struct
STUDENT
students[4];students[0].studentID
students[0].scoreComputer它們都是變量,一般稱為結構的成員變量四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第9頁。用戶自定義的數據類型結構體:把關系緊密、且邏輯相關的多種不同類型的變量,組織到統一的名字之下占用相鄰的一段內存單元共用體:把情形互斥、但邏輯相關的多種不同類型的變量,組織到統一的名字之下占用同一段內存單元,每一時刻只有一個數據起作用四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第10頁。10010LiFunM1887.5Beijingnumnamesexagescoreaddr形成一個樣板用于生成結構體變量struct
結構體名{
類型關鍵字
成員名1;
類型關鍵字成員名2;…...
類型關鍵字成員名n;};構成結構體的變量稱結構體成員(member)也稱域(filed)結構體類型的定義Don’tforgetthesemicolon!!structstudent{
intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];};四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第11頁。10010LiFunM1887.5Beijingnumnamesexagescoreaddrstructstudent{
intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];};struct
結構體名{
類型關鍵字
成員名1;
類型關鍵字成員名2;…...
類型關鍵字成員名n;};結構體類型的定義只定義了數據的形式,即聲明了一種復雜的數據類型,并未生成任何變量。四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第12頁。先定義結構體類型再定義變量名在定義類型的同時定義變量直接定義結構體變量(不出現結構體名)structstudent
student1,student2;structstudent{
intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];}student1,student2;struct
{
intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];}student1,student2;結構體變量的定義四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第13頁。定義自己的類型名struct
student
student1,student2;/*Itworks*/student
student1,student2;/*Canthiswork?*/
typedef
struct
student
STUD;STUD
student1,student2;
/*Itworks!*/typedef為一種已存在的類型定義一個新名字STUD與struct
student類型是同義詞structstudent{
intnum;
charname[20];
charsex;
intage;
floatscore;
charaddr[30];};四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第14頁。struct類型的特點是一個普通的類型可以定義該類型的變量、數組、指針……可以做函數的參數類型和返回值類型它的成員可以是任意類型基本類型、數組、指針、結構體、共用體……struct類型的變量兩個結構體變量之間可以相互賦值所以做為函數的參數時,是傳值調用可以取地址&不可直接參與算術和比較運算面向對象和數據庫是struct思想的發展四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第15頁。結構體的內存占用double占用內存字節數=8struct類型用內存字節數=?是所有成員變量的內存總和嗎?structnumber{
shorti;
charch;
floatf;};printf("%d\n",sizeof(structnumber));用運算符sizeof獲得結構體大小sizeof(變量或表達式)sizeof(類型)8Why?四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第16頁。結構體的內存占用事實上所有數據類型在內存中都是從偶數地址開始存放的且結構所占的實際空間一般是按照機器字長對齊的不同的編譯器、平臺,對齊方式會有變化結構體變量的成員的存儲對齊規則是與機器相關的具有特定數據類型的數據項的大小也是與機器相關的所以一個結構體在內存中的存儲格式也是與機器相關的ifchichf非所有成員變量的內存總和8個字節四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第17頁。structSTUDENT{
int studentID;
char studentName[10];
char studentSex[4];
structdatetimeOfEnter;
int scoreComputer;
int scoreEnglish;
int scoreMath;
int scoreMusic;};
structSTUDENTstu[30];
結構體數組學號姓名性別入學時間計算機原理年月日英語數學音樂structdate{
intyear;
intmonth;
intday;};
結構體定義可以嵌套四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第18頁。structSTUDENTstu[30]={{1,"令狐沖","男",{1999,08,26},90,83,72,82},{2,"林平之","男",{1999,08,26},78,92,88,78},{3,"岳靈珊","女",{1999,08,26},89,72,98,66},{4,"任盈盈","女",{1999,08,26},78,95,87,90}};初始化結構體數組structSTUDENT{
int studentID;
char studentName[10];
char studentSex[4];
structdatetimeOfEnter;
int scoreComputer;
int scoreEnglish;
int scoreMath;
int scoreMusic;};
四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第19頁。structSTUDENTstu[30]={{1,"令狐沖","男",{1999,08,26},90,83,72,82},{2,"林平之","男",{1999,08,26},78,92,88,78},{3,"岳靈珊","女",{1999,08,26},89,72,98,66},{4,"任盈盈","女",{1999,08,26},78,95,87,90}};初始化1令狐沖男9019998268072822林平之男7819998269288783岳靈珊女8919998267298664任盈盈女781999826958790建立了數據庫中的多條記錄,每條對應一個學生信息四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第20頁。結構體變量的指針pStustustructSTUDENT{
int studentID;
char studentName[10];
char studentSex[4];
structdatetimeOfEnter;
int scoreComputer;
int scoreEnglish;
int scoreMath;
int scoreMusic;};
struct
STUDENT
stu;/*定義結構體變量*/struct
STUDENT
*pStu;/*定義結構體指針*/
pStu=&stu;成員1成員2成員3成員4成員5成員6成員7成員8四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第21頁。pStustustructSTUDENT{
int studentID;
char studentName[10];
char studentSex[4];
structdatetimeOfEnter;
int scoreComputer;
int scoreEnglish;
int scoreMath;
int scoreMusic;};
struct
STUDENT
stu;/*定義結構體變量*/struct
STUDENT
*pStu;/*定義結構體指針*/
pStu=&stu;成員1成員2成員3成員4成員5成員6成員7成員8如何訪問結構體的成員通過stu和成員運算符訪問結構體成員stu.studentID=1;通過pStu和指向運算符訪問結構體成員(*pStu).studentID=1;pStu->studentID=1;四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第22頁。structSTUDENT{
int studentID;
char studentName[10];
char studentSex[4];
structdatetimeOfEnter;
int scoreComputer;
int scoreEnglish;
int scoreMath;
int scoreMusic;};
struct
STUDENT
stu;/*定義結構體變量*/struct
STUDENT
*pStu;/*定義結構體指針*/
pStu=&stu;如何訪問嵌套的結構體的成員stu.
timeOfEnter.
year=1999;(*pStu).
timeOfEnter.
year=1999;pStu->
timeOfEnter.
year=1999;structdate{
intyear;
intmonth;
intday;};
四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第23頁。思考題structpoint
{
intx;
inty;
};
structrect
{
structpointpt1;
structpointpt2;
};structrectrt;structrect*rp=&rt;下面表達式哪些合法?rt.pt1.xrp.pt1.x(*rp).pt1.x(&rt)->pt1.xrp->pt1.xrt->pt1.x上面合法的表達式都是等價的嗎?四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第24頁。structSTUDENTstu[4]={{1,"令狐沖","男",{1999,08,26},90,83,72,82},{2,"林平之","男",{1999,08,26},78,92,88,78},{3,"岳靈珊","女",{1999,08,26},89,72,98,66},{4,"任盈盈","女",{1999,08,26},78,95,87,90}};結構體數組的指針pStustu[4]structSTUDENT{
int studentID;
char studentName[10];
char studentSex[4];
structdatetimeOfEnter;
int scoreComputer;
int scoreEnglish;
int scoreMath;
int scoreMusic;};
structSTUDENT*pStu;/*定義結構體指針*/pStu=stu;/*相當于pStu=&stu[0];*/stu[0]stu[1]stu[2]stu[3]四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第25頁。使用pStu++,使pStu指向stu[i]pStu->studentID等價于
stu[i].studentID如何訪問結構體數組元素的成員pStustu[4]structSTUDENT{
int studentID;
char studentName[10];
char studentSex[4];
structdatetimeOfEnter;
int scoreComputer;
int scoreEnglish;
int scoreMath;
int scoreMusic;};
structSTUDENT*pStu;/*定義結構體指針*/pStu=stu;/*相當于pStu=&stu[0];*/stu[0]stu[1]stu[2]stu[3]四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第26頁。向函數傳遞結構體向函數傳遞結構體的單個成員復制單個成員的內容函數內對結構內容的修改不影響原結構向函數傳遞結構體的完整結構?向函數傳遞結構體的首地址?四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第27頁。structdate{
intyear;
intmonth;
intday;};voidfunc(structdate
p){
p.year=2000;
p.month=5;
p.day=22;}main(){
structdated;d.year=1999;d.month=4;d.day=23;printf(“%d,%d,%d\n”,d.year,d.month,d.day);func(d);
printf(“%d,%d,%d\n”,d.year,d.month,d.day);}1999,4,231999,4,23結構體變量
做函數參數四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第28頁。structdate{
intyear;
intmonth;
intday;};voidfunc(structdate
*p){
p->year=2000;
p->month=5;
p->day=22;}main(){
structdated;d.year=1999;d.month=4;d.day=23;printf(“%d,%d,%d\n”,d.year,d.month,d.day);func(&d);
printf(“%d,%d,%d\n”,d.year,d.month,d.day);}1999,4,232000,5,22結構體指針
做函數參數指針做函數形參實參必須為地址值四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第29頁。structdate{
intyear;
intmonth;
intday;};structdatefunc(structdate
p){
p.year=2000;
p.month=5;
p.day=22;}main(){
structdated;d.year=1999;d.month=4;d.day=23;printf(“%d,%d,%d\n”,d.year,d.month,d.day);
d=func(d);
printf(“%d,%d,%d\n”,d.year,d.month,d.day);}結構體變量
做函數返回值存在一個錯誤四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第30頁。structdate{
intyear;
intmonth;
intday;};structdatefunc(structdate
p){
p.year=2000;
p.month=5;
p.day=22;
returnp;}main(){
structdated;d.year=1999;d.month=4;d.day=23;printf(“%d,%d,%d\n”,d.year,d.month,d.day);
d=func(d);
printf(“%d,%d,%d\n”,d.year,d.month,d.day);}1999,4,232000,5,22結構體變量
做函數返回值四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第31頁。向函數傳遞結構體向函數傳遞結構體的完整結構復制整個結構體成員的內容,多個值函數內對結構內容的修改不影響原結構內容傳遞更直觀,但開銷大向函數傳遞結構體的首地址用結構體數組/結構體指針做函數參數僅復制結構體的首地址,一個值修改結構體指針所指向的結構體的內容指針傳遞效率高四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第32頁。思考下面的結構是什么意思?structtemp
{
int data;
structtemppt;
};CB下的錯誤提示:field'pt'hasincompletetypeTC下的錯誤提示:Undefinedstructure‘temp’StructuresizetoolargeVC下的錯誤提示:'pt'usesundefinedstruct‘temp’下面的結構是什么意思呢?structtemp
{
intdata;
structtemp*pt;
};結構體聲明時不能包含本結構體類型成員系統將無法為這樣的結構體類型分配內存
可包含指向本結構體類型的指針變量四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第33頁。共用體,或稱為聯合(Union)structnumber{
shorti;
charch;
floatf;};0x0037b00unionnumber{
shorti;
charch;
floatf;};printf("%d\n",sizeof(structnumber));ichf8個字節ichf4個字節printf("%d\n",sizeof(unionnumber));四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第34頁。共用體,或稱為聯合(Union)sizeof(unionnumber)取決于占空間最多的那個成員變量0x0037b00同一內存單元在每一瞬時只能存放其中一種類型的成員;起作用的成員是最后一次存放的成員不能作為函數參數不能進行比較操作,只能對第一個成員初始化f4個字節四川大學《c語言程序設計》課件-第8章-結構體與共用體全文共39頁,當前為第35頁。structperson{
charname[20];
charsex;
intage;
union
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 苯胺產業實施方案
- 不育癥的遺傳學機制
- 腦梗塞護理指南
- 湖北省孝感市孝昌縣2024-2025學年八年級下學期6月期末數學試題(含部分答案)
- 2025年天津市南大附中高一期中-政治試卷
- 小班便當活動方案
- 岐山宣傳活動方案
- 巴士廣告策劃活動方案
- 小航空公司團購活動方案
- 小學積木校園活動方案
- 《中藥學》課件-中藥思政元素案例
- 醫院保潔服務投標方案(完整技術標)
- 廣東省深圳市寶安區2022-2023學年二年級下學期期末數學試卷
- 譯林版英語八年級下冊語法知識總結
- 范卿平人教版初三化學講義全集
- 幼兒園規范化幼兒園參評自評報告
- 產科運用PDCA循環降低入室新生兒低血糖發生率品管圈成果匯報
- 《水資源管理》機考題庫及答案開放大學考試題庫 答案
- 文件簽收回執單
- 衛生經濟學智慧樹知到答案章節測試2023年華中科技大學
- 替普瑞酮聯合硫糖鋁治療慢性非萎縮性胃炎伴糜爛的療效及安全性分析
評論
0/150
提交評論