C語言struct結(jié)構(gòu)體介紹_第1頁
C語言struct結(jié)構(gòu)體介紹_第2頁
C語言struct結(jié)構(gòu)體介紹_第3頁
C語言struct結(jié)構(gòu)體介紹_第4頁
C語言struct結(jié)構(gòu)體介紹_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

第C語言struct結(jié)構(gòu)體介紹目錄structstruct的嵌套實驗

struct

C語言沒有其他語言的對象(object)和類(class)的概念,struct結(jié)構(gòu)很大程度上提供了對象和類的功能。

下面是struct自定義數(shù)據(jù)類型的一個例子。

structtag{

member-list

member-list

member-list

}variable-list;

聲明了數(shù)據(jù)類型car和該類型的變量car。

structcar

char*name;

floatprice;

intspeed;

}mycar;

structcarmyca={"大眾",178.9,100};

="本田";

如果將struct變量傳入函數(shù),函數(shù)內(nèi)部得到的是一個原始值的副本。

#includestdio.h

structturtle{

char*name;

char*species;

intage;

voidhappy(structturtlet){

t.age=t.age+1;

intmain(){

structturtlemyTurtle={"MyTurtle","seaturtle",99};

happy(myTurtle);

printf("Ageis%i\n",myTurtle.age);//輸出99

return0;

}

上面示例中,函數(shù)happy()傳入的是一個struct變量myTurtle,函數(shù)內(nèi)部有一個自增操作。但是,執(zhí)行完happy()以后,函數(shù)外部的age屬性值根本沒變。原因就是函數(shù)內(nèi)部得到的是struct變量的副本,改變副本影響不到函數(shù)外部的原始數(shù)據(jù)。

指針變量也可以指向struct結(jié)構(gòu)。

structbook{

chartitle[500];

charauthor[100];

floatvalue;

}*b1;

上面示例中,變量b1是一個指針,指向的數(shù)據(jù)是structbook類型的實例。

為了使用指向該結(jié)構(gòu)的指針訪問結(jié)構(gòu)的成員,必須使用-運算符,如下所示:

b1-title;//9-2.c

structBooks

chartitle[50];

charauthor[50];

charsubject[100];

intbook_id;

//函數(shù)聲明

voidprintBook(structBooks*books);

intmain()

structBooksBook1;

structBooksBook2;

*Book1描述

strcpy(Book1.title,"CProgramming");

strcpy(Book1.author,"NuhaAli");

strcpy(Book1.subject,"CProgrammingTutorial");

Book1.book_id=6495407;

/*Book2詳述*/

strcpy(Book2.title,"TelecomBilling");

strcpy(Book2.author,"ZaraAli");

strcpy(Book2.subject,"TelecomBillingTutorial");

Book2.book_id=6495700;

/*通過傳Book1的地址來輸出Book1信息*/

printBook(Book1);

/*通過傳Book2的地址來輸出Book2信息*/

printBook(Book2);

return0;

voidprintBook(structBooks*book)

printf("Booktitle:%s\n",book-title);

printf("Bookauthor:%s\n",book-author);

printf("Booksubject:%s\n",book-subject);

printf("Bookbeforebook_id:%d\n",book-book_id);

(*book).book_id=(*book).book_id+1;

printf("Bookagterbook_id:%d\n",book-book_id);

}

struct結(jié)構(gòu)也可以作為數(shù)組成員。下面示例聲明了一個有1000個成員的數(shù)組books,每個成員都是自定義類型book的實例。

structBooks

chartitle[50];

charauthor[50];

charsubject[100];

intbook_id;

intmain(intargc,charconst*argv[])

structBooksbooks[1000];

books[0].book_id=22;

books[0].book_id=7;

return0;

}

struct的嵌套

struct結(jié)構(gòu)的成員可以是另一個struct結(jié)構(gòu)。

structspecies{

char*name;

intkinds;

structfish{

char*name;

intage;

structspeciesbreed;

};

上面示例中,fish的屬性breed是另一個struct結(jié)構(gòu)species。

//寫法三

structfishshark={

.name="shark",

.age=9,

.breed={"Selachimorpha",500}

};

引用breed屬性的內(nèi)部屬性,要使用兩次點運算符()。

對字符數(shù)組屬性賦值,要使用strcpy()函數(shù),不能直接賦值,因為直接改掉字符數(shù)組名的地址會報錯。

strcpy(),Harry

struct結(jié)構(gòu)內(nèi)部不僅可以引用其他結(jié)構(gòu),還可以自我引用,即結(jié)構(gòu)內(nèi)部引用當(dāng)前結(jié)構(gòu)。比如,鏈表結(jié)構(gòu)的節(jié)點就可以寫成下面這樣。

structnode{

intdata;

structnode*next;

};

上面示例中,node結(jié)構(gòu)的next屬性,就是指向另一個node實例的指針。下面,使用這個結(jié)構(gòu)自定義一個數(shù)據(jù)鏈表。

//p9-2.c

#includestdio.h

#includestdlib.h

#includestring.h

intmain(intargc,charconst*argv[])

structnode

intdata;

structnode*next;

structnode*head;

//生成一個三個節(jié)點的列表(11)-(22)-(33)

head=malloc(sizeof(structnode));

head-data=11;

head-next=malloc(sizeof(structnode));

head-next-data=22;

head-next-next=malloc(sizeof(structnode));

head-next-next-data=33;

head-next-next-next=NULL;

//遍歷這個列表

for(structnode*cur=head;cur!=NULL;cur=cur-next)

printf("%d\n",cur-data);

return0;

}

實驗

考慮下面發(fā)這些聲明和數(shù)據(jù),并debug

#includestdio.h

intmain(intargc,charconst*argv[])

structNODE

inta;

structNODE*b;

structNODE*c;

//5個成員的數(shù)組nodes

structNODEnodes[5]=

{5,nodes+3,NULL},

{15,nodes+4,nodes+3},

{22,NULL,NULL},

{12,nodes+1,nodes},

{18,nodes+2,nodes+1},

structNODE*np=nodes+2;

structNODE**npp=nodes[1].b;

/*nodes是數(shù)組,數(shù)組名就是該數(shù)組的指針,也是該數(shù)組第一個元素的指針*/

//輸出該數(shù)組的地址

printf("nodes的地址是%p\n",nodes);//0x7ffeefbff460

printf("nodes的地址是%p\n",nodes);

printf("nodes+2的地址是%p\n",nodes+2);

//printf("%d\n",nodes.a)//錯誤;指針訪問屬性需要使用-

printf("nodes[0]a的值%d\n",nodes-//5通過指針訪問

printf("(*nodes).a)的值%d\n",(*nodes).a);//(*nodes)獲取的是nodes[1]

printf("nodes[3]a的值%d\n",nodes[3].a);//12

printf("nodes[3].c的值%p\n",nodes[3].c);//0x7ffeefbff460

//訪問的是nodes[0]

printf("nodes[3].c-a的值%d\n",nodes[3].c-//5

//printf("%d\n",*nodes);使用*操作符對指針執(zhí)行間接訪問,*nodes的右值是nodes的整個結(jié)構(gòu)

//printf("%d\n",*nodes.a);//錯誤

printf("nodes[4]的值地址%p\n",nodes[4]);

printf("nodes[3].b-b的值%p\n",nodes[3].b-//nodes[3].b獲取的是nodes+1即nodes[1]的指針,然后nodes[1]-b,就是nodes[4]的指針

//[]().-是一級運算從左往右邊結(jié)合,*是二級運算

printf("*nodes[3].b-b的值%p\n",*nodes[3].b-//{18,nodes+2,nodes+1},//最后運算*,由前邊可以知道nodes[3].b-b就是nodes[4]的指針,然后*,得到nodes[4]。看下一提題的驗證

printf("nodes[3]a的值%d\n",(*nodes[3].b-b).a);//18

printf("nodes[3].a的值%p\n",nodes[3].a);//數(shù)子12第物理地址

//printf("n?odes[3].c%p\n",nodes[3].c);

printf("nodes[3].c的值%p\n",nodes[3].c-//數(shù)字15的物理地址

printf("nodes-a的值%p\n",nodes-//數(shù)字5的物理地址

printf("nodes+2的地址是%p\n",nodes+2);

printf("np的值%p\n",np);//

溫馨提示

  • 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)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論