初步剖析C語(yǔ)言編程中的結(jié)構(gòu)體
C語(yǔ)言結(jié)構(gòu)體,可謂是C強(qiáng)大功能之一,也是C++語(yǔ)言之所以能衍生的有利條件,事實(shí)上,當(dāng)結(jié)構(gòu)體中成員中有函數(shù)指針了后,那么,結(jié)構(gòu)體也即C++中的類(lèi)了。
C語(yǔ)言中,結(jié)構(gòu)體的聲明、定義是用到關(guān)鍵字struct,就像聯(lián)合體用到關(guān)鍵字union、枚舉類(lèi)型用到enum關(guān)鍵字一樣,事實(shí)上,聯(lián)合體、枚舉類(lèi)型的用法幾乎是參照結(jié)構(gòu)體來(lái)的。結(jié)構(gòu)體的聲明格式如下:
struct tag-name{ { member 1; … member N; };
因此,定義結(jié)構(gòu)體變量的語(yǔ)句為:struct tag-name varible-name,如struct point pt;其中,point 為tag-name,pt是結(jié)構(gòu)體struct point變量。當(dāng)然,也可以一次性聲明結(jié)構(gòu)體類(lèi)型和變量,即如下:struct tag-name {…} x,y,z;就類(lèi)似于int x,y,z;語(yǔ)句一樣。也可以在定義結(jié)構(gòu)體變量時(shí)即賦初值,即變量初始化,struct point pt={320,200};
當(dāng)然,也就可以有結(jié)構(gòu)體指針、結(jié)構(gòu)體數(shù)組了。訪問(wèn)結(jié)構(gòu)體變量中的member的方法有:如果是由結(jié)構(gòu)體變量名來(lái)訪問(wèn),則是structure-variable-name.member;如果是由結(jié)構(gòu)體變量指針來(lái)訪問(wèn),則是structure-variable-pointer->member;
好了,上面的不是重點(diǎn),也不難掌握,只是細(xì)節(jié)問(wèn)題。結(jié)構(gòu)體具有重要的應(yīng)用,如下的:
如自引用的結(jié)構(gòu)體,常用來(lái)作為二叉樹(shù)等重要數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn):假設(shè)我們要實(shí)現(xiàn)一個(gè)普遍的問(wèn)題的解決算法——統(tǒng)計(jì)某些輸入的各單詞出現(xiàn)的頻數(shù)。由于輸入的單詞數(shù)是未知,內(nèi)容未知,長(zhǎng)度未知,我們不能對(duì)輸入進(jìn)行排序并采用二分查找?!敲?,一種解決辦法是:將已知的單詞排序——通過(guò)將每個(gè)到達(dá)的單詞排序到適當(dāng)位置。當(dāng)然,實(shí)現(xiàn)此功能不能通過(guò)線性排序,因?yàn)槟菢佑锌赡芎荛L(zhǎng),相應(yīng)地,我們將使用二叉樹(shù)來(lái)實(shí)現(xiàn)。該二叉樹(shù)每一個(gè)單詞為一個(gè)二叉樹(shù)結(jié)點(diǎn),每個(gè)結(jié)點(diǎn)包括:
- a pointer to the text of the word
- a count of the number of occurences
- a pointer to the left child node
- a pointer to the right child node
其寫(xiě)在程序中,即:
struct tnode{/*the tree node:*/ char *word;/*points to the next*/ int count;/*number of occurences*/ struct tnode *left;/*left child*/ struct tnode *right;/*right child*/ }
完成上述功能的完整程序如下:
#include<stdio.h> #include<ctype.h> #include<string.h> #include"tNode.h" #define MAXWORD 100 struct tnode *addtree(struct tnode *,char *); void treeprint(struct tnode *); int getword(char *,int); struct tnode *talloc(void); char *strdup2(char *); /*word frequency count*/ main() { struct tnode *root; char word[MAXWORD]; root=NULL; while(getword(word,MAXWORD)!=EOF) if(isalpha(word[0])) root=addtree(root,word); treeprint(root); return 0; } #define BUFSIZE 100 char buf[BUFSIZE];/*buffer for ungetch*/ int bufp=0;/*next free position in buf*/ int getch(void)/*get a (possibly pushed back) character*/ { return (bufp>0)? buf[--bufp]:getchar(); } void ungetch(int c)/*push back character on input*/ { if(bufp>=BUFSIZE) printf("ungetch:too many characters\n"); else buf[bufp++]=c; } /*getword:get next word or character from input*/ int getword(char *word,int lim) { int c,getch(void); void ungetch(int); char *w=word; while(isspace(c=getch() )); if(c!=EOF) *w++=c; if(!isalpha(c)){ *w='\0'; return c; } for(;--lim>0;w++) if(!isalnum(*w=getch())){ ungetch(*w); break; } *w='\0'; return word[0]; } /*addtree:add a node with w,at or below p*/ struct tnode *addtree(struct tnode *p,char *w) { int cond; if(p==NULL){/*a new word has arrived*/ p=talloc();/*make a new node*/ p->word=strdup(w); p->count=1; p->left=p->right=NULL; }else if((cond=strcmp(w,p->word))==0) p->count++;/*repeated word*/ else if(cond<0)/*less than into left subtree*/ p->left=addtree(p->left,w); else /*greater than into right subtree*/ p->right=addtree(p->right,w); return p; } /*treeprint:in-order print of tree p*/ void treeprint(struct tnode *p) { if(p!=NULL){ treeprint(p->left); printf("%4d %s\n",p->count,p->word); treeprint(p->right); } } #include<stdlib.h> /*talloc:make a tnode*/ struct tnode *talloc(void) { return (struct tnode *)malloc(sizeof(struct tnode)); } char *strdup2(char *s)/*make a duplicate of s*/ { char *p; p=(char *)malloc(strlen(s)+1);/*+1 for '\0'*/ if(p!=NULL) strcpy(p,s); return p; }
其中,其它的關(guān)于union、enum這里就不多說(shuō)了,再說(shuō)一個(gè)關(guān)于結(jié)構(gòu)體的非常重要的應(yīng)用——位操作:
當(dāng)然,我們知道,對(duì)于位操作,我們可通過(guò)#define tables(即用宏和C中的位操作來(lái)實(shí)現(xiàn))
如:
#define KEYWORD 01 /*0001*/ #define EXTERNAL 02 /*0010*/ #define STATIC 04 /*0100*/
或
enum{KEYWORD =01,EXTERNAL =02,STATIC =04};
那么,flags|=EXTERNAL|STATIC;將打開(kāi)flags的EXTERNAL和STATIC位,而
flags&=~(EXTERNAL|STATIC);將關(guān)閉flags的EXTERNAL和STATIC位.
然而,上述定義的位模式可以用結(jié)構(gòu)體如下寫(xiě):
struct{ unsigned int is_keyword:1; unsigned int is_extern:1; unsigned int is_static:1; }flags;/*This defines a variable called flags that contains three 1-bit fields*/
那么,上述打開(kāi)相應(yīng)位的操作為:
flags.is_extern=flags.is_static=1;
上述關(guān)閉相應(yīng)位的操作為:
flags.is_extern=flags.is_static=0;
- 深入分析C語(yǔ)言中結(jié)構(gòu)體指針的定義與引用詳解
- 淺談C語(yǔ)言中結(jié)構(gòu)體的初始化
- php讀取二進(jìn)制流(C語(yǔ)言結(jié)構(gòu)體struct數(shù)據(jù)文件)的深入解析
- C語(yǔ)言中的結(jié)構(gòu)體的入門(mén)學(xué)習(xí)教程
- 詳解C語(yǔ)言的結(jié)構(gòu)體中成員變量偏移問(wèn)題
- C語(yǔ)言中結(jié)構(gòu)體struct編寫(xiě)的一些要點(diǎn)解析
- 詳解C語(yǔ)言中的內(nèi)存四區(qū)模型及結(jié)構(gòu)體對(duì)內(nèi)存的使用
- 詳解C語(yǔ)言結(jié)構(gòu)體中的函數(shù)指針
- C語(yǔ)言中結(jié)構(gòu)體偏移及結(jié)構(gòu)體成員變量訪問(wèn)方式的問(wèn)題討論
- C語(yǔ)言 結(jié)構(gòu)體數(shù)組詳解及示例代碼
相關(guān)文章
python 數(shù)據(jù)類(lèi)型強(qiáng)制轉(zhuǎn)換的總結(jié)
這篇文章主要介紹了python 數(shù)據(jù)類(lèi)型強(qiáng)制轉(zhuǎn)換的使用總結(jié),幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01使用python實(shí)現(xiàn)男神女神顏值打分系統(tǒng)(推薦)
這篇文章主要介紹了用python做一個(gè)男神女神顏值打分系統(tǒng)(程序分析見(jiàn)注釋),需要的朋友可以參考下2019-10-10tensorflow學(xué)習(xí)筆記之簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)訓(xùn)練和測(cè)試
這篇文章主要為大家詳細(xì)介紹了tensorflow學(xué)習(xí)筆記,用簡(jiǎn)單的神經(jīng)網(wǎng)絡(luò)來(lái)訓(xùn)練和測(cè)試,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04pyinstaller打包python3.6和PyQt5中各種錯(cuò)誤的解決方案匯總
pyinstaller是打包python很方便的一個(gè)套件,我們可以很輕易地使用他,下面這篇文章主要給大家介紹了關(guān)于pyinstaller打包python3.6和PyQt5中各種錯(cuò)誤解決的相關(guān)資料,需要的朋友可以參考下2022-08-08pytorch中函數(shù)tensor.numpy()的數(shù)據(jù)類(lèi)型解析
這篇文章主要介紹了pytorch中函數(shù)tensor.numpy()的數(shù)據(jù)類(lèi)型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07如何使用PyCharm將代碼上傳到GitHub上(圖文詳解)
這篇文章主要介紹了如何使用PyCharm將代碼上傳到GitHub上(圖文詳解),文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04基于Python編寫(xiě)詞云軟件并顯示分詞結(jié)果
這篇文章主要為大家詳細(xì)介紹了如何基于Python編寫(xiě)一個(gè)簡(jiǎn)單的詞云制作軟件并顯示分詞結(jié)果,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-10-10