用戶自己建立數(shù)據(jù)類型_第1頁
用戶自己建立數(shù)據(jù)類型_第2頁
用戶自己建立數(shù)據(jù)類型_第3頁
用戶自己建立數(shù)據(jù)類型_第4頁
用戶自己建立數(shù)據(jù)類型_第5頁
已閱讀5頁,還剩45頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

9.1概述定義和使用結(jié)構(gòu)體變量

9.2使用結(jié)構(gòu)體數(shù)組

9.3結(jié)構(gòu)體指針

9.4用指針處理鏈表(自學(xué))9.5共用體類型

9.6使用枚舉類型

9.7用typedef定義聲明新類型名主要內(nèi)容第九章用戶自己建立數(shù)據(jù)類型

9.1定義和使用結(jié)構(gòu)體變量基本類型:整型、實型、字符型.......有時需要將不同類型的數(shù)據(jù)組合成一個有機的整體,以便于引用。如:

一個學(xué)生有學(xué)號/姓名/性別/年齡/地址等屬性intnum;charname[20]; charsex; intage;charaddr[30];把它們組織成一個組合項,在一個組合項中包含若干個類型不同(當(dāng)然也可以相同)的數(shù)據(jù)項。數(shù)組------由相同類型的數(shù)據(jù)組成集合結(jié)構(gòu)體-----由不同類型的數(shù)據(jù)組成集合構(gòu)造數(shù)據(jù)類型:

由簡單數(shù)據(jù)類型(intfloatchar)構(gòu)造而成的(有機整體)數(shù)據(jù)類型。聲明一個結(jié)構(gòu)體類型的一般形式為:struct

結(jié)構(gòu)體名

{

類型標(biāo)識符成員名1;..................................

類型標(biāo)識符成員名n;};{intnum;charname[20];charsex;intage;floatscore;charaddr[30];};結(jié)構(gòu)體名結(jié)構(gòu)體structstudent用戶自已定義的數(shù)據(jù)類型結(jié)構(gòu)各成員新定義的類型structstudent和int、float...一樣,可用來定義變量。struct

students1,s2;數(shù)據(jù)類型變量名例1:一組相關(guān)變量定義結(jié)構(gòu)體類型例2:一組相關(guān)變量定義結(jié)構(gòu)體類型新數(shù)據(jù)類型結(jié)構(gòu)體成員不同數(shù)據(jù)類型的成員intmonth;intday;intyear;structdate{

intmonth;intday;intyear;};charname[30];floatsalary;structwage{charname[30];floatsalary;};structdatestructwage說明:1、結(jié)構(gòu)體類型是一種構(gòu)造數(shù)據(jù)類型,它與int,char,float等系統(tǒng)定義的基本數(shù)據(jù)類型具有同等地位,是由用戶自行定義的。構(gòu)造的數(shù)據(jù)類型可以用來定義變量。2、用戶自定義的數(shù)據(jù)類型時,系統(tǒng)根據(jù)類型為其分配存儲空間。1、先聲明結(jié)構(gòu)體類型再定義結(jié)構(gòu)體變量2、在聲明結(jié)構(gòu)體類型的同時定義結(jié)構(gòu)體變量3、直接定義結(jié)構(gòu)體變量3種方法定義結(jié)構(gòu)體類型變量:9.1.2定義結(jié)構(gòu)體類型變量先聲明結(jié)構(gòu)體類型再定義結(jié)構(gòu)體變量struct

結(jié)構(gòu)體名

{成員表列};struct

結(jié)構(gòu)體名變量名表列;

structstudent {charname[20];

intage;charaddr[30];};在定義了結(jié)構(gòu)體變量后,系統(tǒng)會為之分配內(nèi)存單元。例如:在VC中s1和s2在內(nèi)存中各占54個字節(jié)(20+4+30=54)。

structstudent

s1,s2;在聲明類型的同時定義結(jié)構(gòu)體變量

struct結(jié)構(gòu)體名{成員表列}變量名表列;

structstudent {charname[20];intage;charaddr[30];}s1,s2;struct

{

成員表列

}變量名表列;struct

{charname[20];

intage;charaddr[30];}s1,s2;直接定義結(jié)構(gòu)體變量即不出現(xiàn)結(jié)構(gòu)體名用無名結(jié)構(gòu)體直接定義變量只能一次結(jié)構(gòu)體類型的幾點說明:1、類型與變量是完全不同的概念。先定義結(jié)構(gòu)類型,再定義該類型的變量。

類型:不可賦值、存貯、運算;系統(tǒng)不分配空間。變量:可賦值、存貯、運算;系統(tǒng)要分配空間。2、成員名可以與程序中的變量同名。3、結(jié)構(gòu)體成員也可為結(jié)構(gòu)體。#include<stdio.h>intmain(){

structstd{

charname[20];longintnum;charsex;}s1,s2;

charname[20];………..}structstud{intnum;charname[20];charsex;

intage;

structdate{intmonth;

intday;

intyear;}birthday;charaddr[30];};

structstuds1;可改寫為:struct

date{intmonth;

intday;

intyear;};Structstud{intnum;charname[20];charsex;

intage;

struct

date

birthday;charaddr[30];}s1;結(jié)構(gòu)體的嵌套:結(jié)構(gòu)體成員也可為結(jié)構(gòu)體structstudent {charname[20];intage;charaddr[30];};structstudents1,s2;結(jié)構(gòu)體變量名.成員名

s2.age定義結(jié)構(gòu)引用成員

成員運算符結(jié)構(gòu)體變量的引用9.1.3結(jié)構(gòu)體變量的初始化和引用1.結(jié)構(gòu)體變量的初始化可在定義時賦初值,也可動態(tài)對成員賦值。#include<stdio.h>structstudent{longintnum;charname[20];charsex;charaddr[20];}a={89031,"LiLin",'M',"123BeijingRoad"};intmain(){printf("No.:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,,a.sex,a.addr);return0;}在main()之外例結(jié)構(gòu)體變量初始化(外部存貯類型)填寫輸出表列#include<stdio.h>intmain(){structstudent{longintnum;charname[20];charsex;charaddr[20];}a={89031,"LiLin",'M',"123Beijingoad"};

printf(“No.:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,,a.sex,a.addr);return0;}在main()之內(nèi)例結(jié)構(gòu)體變量初始化#include<stdio.h>

intmain(){structdate{intmonth;

intday;intyear;}today;printf(“Entertoday’sdate(mmddyyyy)\n”);scanf(“%d%d%d”,

&today.month,&today.day,

&today.year);printf(“today’sdateis%d/%d/%d\n”,

today.month,today.day,today.year

);return0;}例結(jié)構(gòu)體數(shù)據(jù)成員輸入數(shù)據(jù)如何輸入structdate類型變量today的各個數(shù)據(jù)成員#include<string.h>structstudent{charnum[10];charname[8];intscore;}stu,change;intmain(){strcpy(

stu.num,”03983101”);

strcpy(

,”zhao”);stu.score=98;

change=stu

;printf(“%-10s%-10s%4d\n”,change.num,,change.score);return0;}結(jié)構(gòu)體變量間的直接賦值運行結(jié)果:03983101zhao98例:結(jié)構(gòu)體數(shù)據(jù)成員賦值遵守以下規(guī)則:(1)不能將一個結(jié)構(gòu)體變量作為一個整體進(jìn)行輸入和輸出。例如:已定義s1和s2為結(jié)構(gòu)體變量并且它們已有值。printf(″%s,%d,%s,\n″,s1);(2)只可引用最低級成員s1.birthday.days1.birthday.years1.numstruct

date{intmonth;

intday;

intyear;};struct{intnum;charname[20];charsex;

intage;

struct

date

birthday;charaddr[30];}s1,s2;(3)結(jié)構(gòu)體變量的成員可以像普通變量一樣進(jìn)行各種運算(根據(jù)其類型決定可以進(jìn)行的運算)。s2.num=s1.num;s1.age++;++s2.age;(4)地址的引用結(jié)構(gòu)體成員的地址引用scanf(“%d”,&s1.num);

結(jié)構(gòu)體變量地址的引用

structstudents1;

structstudent*p;p=&s1;

printf(“%x%x”,&s1,p);9.2使用結(jié)構(gòu)體數(shù)組9.2.1定義結(jié)構(gòu)體數(shù)組和定義結(jié)構(gòu)體變量的方法相仿,只需說明其為數(shù)組即可structstudent{longintnum;charname[20];charsex;

intage;};structstudent

stu[3];stu[3]為結(jié)構(gòu)體數(shù)組有三個下標(biāo)變量stu[0],stu[1],stu[2]每個下標(biāo)變量均為結(jié)構(gòu)體類型變量包含結(jié)構(gòu)體各成員如何定義數(shù)據(jù)類型為structstudent,數(shù)組名為stu,數(shù)組長度為3的數(shù)組?數(shù)組的各元素內(nèi)存中連續(xù)存放10101“LiLin”‘M’1810102“ZhangFun”‘M’1910103“WangMin”‘F’20stu[2]stu[1]stu[0]stu[0]stu[1]stu[2]numnamesexage10101LiLinM1810102ZhangFunM1910103WangMinF20也可以直接定義一個結(jié)構(gòu)體數(shù)組,例如:

structstudent{intnum;…}stu[3];或:

strcut{intnum;…}stu[3];與其他類型的數(shù)組一樣,對結(jié)構(gòu)體數(shù)組可以初始化。

形式:在定義數(shù)組后面加上={初值表列}structstudent

{intnum;charname[20];charsex;intage;floatscore;charaddr[30];}

stu[3]={{10101,“LiLin”,’M’,18,87.5,”103BeingRoad”},{...},{....}};

定義結(jié)構(gòu)數(shù)組的同時進(jìn)行初始化根據(jù)數(shù)組連續(xù)存放的原則:由結(jié)構(gòu)體常量的個數(shù)來確定數(shù)組元素的個數(shù)按存儲順序初始化

1、數(shù)組元素的個數(shù)可以不寫。

stu[]={{10101,“Liin”,’M’,18,87.5,”103BeingRoad”},{...},{....}};2、數(shù)組元素全部初始化的數(shù)組,內(nèi)層{}也可不寫

structperson{charname[20];intcount;}leader[3]={"Li",0,"Zhang",0,"Fun",0};例:

統(tǒng)計投票結(jié)果1.先定義由候選人姓名及票數(shù)兩個成員構(gòu)成的結(jié)構(gòu)體數(shù)組再定義字符串?dāng)?shù)組變量,放投票結(jié)果;2.初始化數(shù)組:候選人的姓名、票數(shù)(初始值0);3.循環(huán)讀投票結(jié)果給字符串?dāng)?shù)組元素,是候選人:將其票數(shù)+1;否則再循環(huán)。4.打印結(jié)構(gòu)數(shù)組#include<stdio.h>#include<string.h>structperson{charname[20];

intcount;}

leader[3]={"Li",0,"Zhang",0,"Fun",0};intmain(){inti,j;charleader_name[20];for(i=1;i<=10;i++){scanf("%s",leader_name);for(j=0;j<3;j++)if(strcmp(leader_name,leader[j].name

)==0)leader[j].count++;}

printf("\n");for(i=0;i<3;i++)printf("%5s:%d\n",leader[i].name,leader[i].count);return0;}9.3結(jié)構(gòu)體指針結(jié)構(gòu)體的指針就是該結(jié)構(gòu)變量在內(nèi)存中所占的起始地址。結(jié)構(gòu)體類型的指針:可指向結(jié)構(gòu)體變量,也可指向結(jié)構(gòu)體數(shù)組中的數(shù)組元素。例:指向結(jié)構(gòu)體變量的指針的應(yīng)用#include<string.h>#include<stdio.h>intmain(){structstudent{longintnum;charname[20];charsex;floatscore;};

structstudentstu_1;

structstudent*p;p=&stu_1;

stu_1.num=89101;strcpy(stu_1.name,"LiLin");stu_1.sex='M';stu_1.score=89.5;

如何定義指針變量p,指向structstudent類型的數(shù)據(jù)?9.3.1指向結(jié)構(gòu)體變量的指針如何通過指向stu_1的指針變量p,引用其數(shù)據(jù)成員?printf("\n\nNo.:%ld\nname:%s\nsex:%c\nscore:%f\n",

stu_1.num,stu_1.name,stu_1.sex,stu_1.score);printf("\nNo.:%ld\nname:%s\nsex:%c\nscore:%f\n",(*p).num,(*p).name,(*p).sex,(*p).score);printf("\nNo.:%ld\nname:%s\nsex:%c\nscore:%f\n",p->num,p->name,p->sex,p->score);return0;}p指向的結(jié)構(gòu)體變量中的成員對結(jié)構(gòu)體變量中成員引用的三種方法:1、結(jié)構(gòu)體變量名.成員名

std.num;2、(*p).成員名

(*p).num3、p->成員名

p->num

.結(jié)構(gòu)成員運算符

->指向運算符優(yōu)先級:1

結(jié)合性:從左至右structstudentstd,*p=&std;structstudent{longintnum;charname[20];charsex;floatscore;};

structstudentstd;

例:有定義:struct{intn; charch;}count,*p;p=&count;得到成員n,用完后n加1得到成員n的值,n先加1后使用p->n(++p)->np->n++++p->n指針p先加1,再訪問np所指向結(jié)構(gòu)體變量的成員n的值

用于結(jié)構(gòu)體數(shù)組,指向下一個結(jié)構(gòu)體數(shù)組元素9.3.2指向結(jié)構(gòu)體數(shù)組的指針例:指向結(jié)構(gòu)體數(shù)組的指針的應(yīng)用#include<stdio.h>structstudent{intnum;charname[20];charsex;

intage;};structstudentstu[3]={{10101,"LiLin",'M',18}, {10102,"ZhangFun",'M',19},{10103,"WangMin",'F',20}};

intmain(){

structstudent*p;

printf("NoNamesexage\n");for(

p=stu

;p<stu+3;

p++

)printf("%5d%-10s%2c%4d\n",

p->num,p->name,p->sex,p->age);return0;}p++指向結(jié)構(gòu)體數(shù)組下一個數(shù)組元素,而不是下一個結(jié)構(gòu)體成員structstudent類型的數(shù)組stu[3]的存儲示意圖:10101“LiLin”‘M’1810102“ZhangFun”‘M’1910103“WangMin”‘F’20stu[2]stu[1]stu[0]pp+1p+2p=stu;p為指向structstudent類型的指針p++將指向結(jié)構(gòu)體數(shù)組的下一個數(shù)組元素,而不是下一個結(jié)構(gòu)體成員。

p指針只可賦結(jié)構(gòu)體的地址,如果要指向某一成員,則必須進(jìn)行強制轉(zhuǎn)換。

p=(structstudent*)&stu[1].num;9.3.3用結(jié)構(gòu)體變量和結(jié)構(gòu)體變量的指針作函數(shù)參數(shù)將結(jié)構(gòu)變量的值傳給另一函數(shù):

1、結(jié)構(gòu)成員作參數(shù)-------傳值

用法和普通變量相同

2、指向結(jié)構(gòu)變量的指針作參數(shù)

---傳地址

使用結(jié)構(gòu)體變量(或數(shù)組)的指針作函數(shù)實參,其對應(yīng)的形參應(yīng)是相同結(jié)構(gòu)體類型的指針變量。

3、整個結(jié)構(gòu)體變量作參數(shù)---傳結(jié)構(gòu)實參和形參間采用值復(fù)制方式,運行效率低,不提倡。例:有一個結(jié)構(gòu)體變量stu,內(nèi)含學(xué)生學(xué)號、姓名和3門課程的成績。要求在main函數(shù)中賦予值,在另一函數(shù)print中將它們輸出。今用指向結(jié)構(gòu)體變量的指針作實參#include<stdio.h>#include<string.h>#define

format"%d\n%s\n%f\n%f\n%f\n"structstudent{intnum;charname[20];floatscore[3];};intmain(){voidprint(structstudent*);

structstudentstu;

stu.num=12345;

strcpy(,"LiLi");stu.score[0]=67.5;stu.score[1]=89;stu.score[2]=78.6;print(&stu);return0;}定義外部結(jié)構(gòu)體類型同一個文件中的各函數(shù)均可用它來定義該結(jié)構(gòu)體變量voidprint(structstudent*p){

printf(format,p->num,p->name,p->score[0],p->score[1],p->score[2]);

printf("\n");}score[0]numnamescore[1]score[2]pP指向stu#include<stdio.h>intmain(){structstudent{intnum;charname[20];floatscore;};

structstudentstu[4];

structstudent*p;

inti,temp=0;floatmax;for(i=0;i<4;i++)

scanf("%d%s%f",

&stu[i].num,stu[i].name,&stu[i].score);for(max=stu[0].score,i=1;i<4;i++)if(stu[i].score>max){max=stu[i].score;

temp=i;}

p=stu+temp;

printf(“\nTheMaxnumscore:\n”;

printf("%d\%s\n%4.1f\n",p->num,p->name,p->score);}程序的功能是什么?9.5共用體類型一、共用體的概念使幾個不同的變量共占同一段內(nèi)存的結(jié)構(gòu)稱為“共用體”類型的結(jié)構(gòu)。共用體變量的定義同結(jié)構(gòu)體一樣有三種形式:uniondata{charch;floatf;inti;};uniondataa,b,c;uniondata{charch;floatf;inti;}a,b,c;union{charch;floatf;inti;}a,b,c;共用體變量x占用內(nèi)存示意圖:(1)采用內(nèi)存覆蓋技術(shù),使不同數(shù)據(jù)類型的各個成員變量共用同一存儲區(qū)。(2)共用體變量所占內(nèi)存長度為其最長成員的長度fich地址1000結(jié)構(gòu)體與共用體所占內(nèi)存長度結(jié)構(gòu)體變量:各數(shù)據(jù)成員所占內(nèi)存之和共用體:數(shù)據(jù)成員所占內(nèi)存長度最大值//TC環(huán)境union{charch;floatf;inti;}x;二、共用體變量的引用方式共用體變量的引用方法類似于結(jié)構(gòu)體變量。

共用體變量名.成員變量名例:unionmixed{charch;floatf;

inti;};unionmixedx;不能引用共用體變量,只能引用其成員。

對x變量各成員的引用:

x.c

x.f

x.i三、共用體類型數(shù)據(jù)的特點1、同一段內(nèi)存空間可供幾種不同類型的成員共享,但在某一時刻只能存放其中的一個成員。2、共用體變量中起作用的成員是最后一次存放的成員,在存入一新成員后原有成員就失去作用。uniondata{ charch;

inti;};voidmain(){ uniondataa;

a.i=256;

printf("\nThevalueofa.iis:%d",a.i);

a.ch='a';

printf("\nThevalueofa.iis:%d",a.i);}例如:共用體變量內(nèi)存使用說明(TC環(huán)境)運行結(jié)果:a.i=2560000000000000001a.ch=‘a(chǎn)’01100001000000013、共用體變量的地址和它的各成員的地址相同。4、不能對共用體變量名賦值,也不可能引用變量名來得到成員的值。不能在定義時對它初始化。5、共用體變量不能作為函數(shù)的參數(shù),也不能使函數(shù)帶回共用體變量。6、共用體類型可以出現(xiàn)在結(jié)構(gòu)體類型中,也可以定義共用體數(shù)組。9.6枚舉類型枚舉類型的概念:

枚舉是某些常量的集合,枚舉變量只可取集合內(nèi)的值。定義枚舉類型和枚舉變量:

enumweek{sun,mon,tue,wed,thu,fri,sat};

enumweekday;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論