初步剖析C语言编程中的结构体
C语言结构体,可谓是C强大功能之一,也是C++语言之所以能衍生的有利条件,事实上,当结构体中成员中有函数指针了后,那么,结构体也即C++中的类了。
C语言中,结构体的声明、定义是用到关键字struct,就像联合体用到关键字union、枚举类型用到enum关键字一样,事实上,联合体、枚举类型的用法几乎是参照结构体来的。结构体的声明格式如下:
structtag-name{
{
member1;
…
memberN;
};
因此,定义结构体变量的语句为:structtag-namevarible-name,如structpointpt;其中,point为tag-name,pt是结构体structpoint变量。当然,也可以一次性声明结构体类型和变量,即如下:structtag-name{…}x,y,z;就类似于intx,y,z;语句一样。也可以在定义结构体变量时即赋初值,即变量初始化,structpointpt={320,200};
当然,也就可以有结构体指针、结构体数组了。访问结构体变量中的member的方法有:如果是由结构体变量名来访问,则是structure-variable-name.member;如果是由结构体变量指针来访问,则是structure-variable-pointer->member;
好了,上面的不是重点,也不难掌握,只是细节问题。结构体具有重要的应用,如下的:
如自引用的结构体,常用来作为二叉树等重要数据结构的实现:假设我们要实现一个普遍的问题的解决算法——统计某些输入的各单词出现的频数。由于输入的单词数是未知,内容未知,长度未知,我们不能对输入进行排序并采用二分查找。……那么,一种解决办法是:将已知的单词排序——通过将每个到达的单词排序到适当位置。当然,实现此功能不能通过线性排序,因为那样有可能很长,相应地,我们将使用二叉树来实现。该二叉树每一个单词为一个二叉树结点,每个结点包括:
- apointertothetextoftheword
- acountofthenumberofoccurences
- apointertotheleftchildnode
- apointertotherightchildnode
其写在程序中,即:
structtnode{/*thetreenode:*/
char*word;/*pointstothenext*/
intcount;/*numberofoccurences*/
structtnode*left;/*leftchild*/
structtnode*right;/*rightchild*/
}
完成上述功能的完整程序如下:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include"tNode.h"
#defineMAXWORD100
structtnode*addtree(structtnode*,char*);
voidtreeprint(structtnode*);
intgetword(char*,int);
structtnode*talloc(void);
char*strdup2(char*);
/*wordfrequencycount*/
main()
{
structtnode*root;
charword[MAXWORD];
root=NULL;
while(getword(word,MAXWORD)!=EOF)
if(isalpha(word[0]))
root=addtree(root,word);
treeprint(root);
return0;
}
#defineBUFSIZE100
charbuf[BUFSIZE];/*bufferforungetch*/
intbufp=0;/*nextfreepositioninbuf*/
intgetch(void)/*geta(possiblypushedback)character*/
{
return(bufp>0)?buf[--bufp]:getchar();
}
voidungetch(intc)/*pushbackcharacteroninput*/
{
if(bufp>=BUFSIZE)
printf("ungetch:toomanycharacters\n");
else
buf[bufp++]=c;
}
/*getword:getnextwordorcharacterfrominput*/
intgetword(char*word,intlim)
{
intc,getch(void);
voidungetch(int);
char*w=word;
while(isspace(c=getch()));
if(c!=EOF)
*w++=c;
if(!isalpha(c)){
*w='\0';
returnc;
}
for(;--lim>0;w++)
if(!isalnum(*w=getch())){
ungetch(*w);
break;
}
*w='\0';
returnword[0];
}
/*addtree:addanodewithw,atorbelowp*/
structtnode*addtree(structtnode*p,char*w)
{
intcond;
if(p==NULL){/*anewwordhasarrived*/
p=talloc();/*makeanewnode*/
p->word=strdup(w);
p->count=1;
p->left=p->right=NULL;
}elseif((cond=strcmp(w,p->word))==0)
p->count++;/*repeatedword*/
elseif(cond<0)/*lessthanintoleftsubtree*/
p->left=addtree(p->left,w);
else/*greaterthanintorightsubtree*/
p->right=addtree(p->right,w);
returnp;
}
/*treeprint:in-orderprintoftreep*/
voidtreeprint(structtnode*p)
{
if(p!=NULL){
treeprint(p->left);
printf("%4d%s\n",p->count,p->word);
treeprint(p->right);
}
}
#include<stdlib.h>
/*talloc:makeatnode*/
structtnode*talloc(void)
{
return(structtnode*)malloc(sizeof(structtnode));
}
char*strdup2(char*s)/*makeaduplicateofs*/
{
char*p;
p=(char*)malloc(strlen(s)+1);/*+1for'\0'*/
if(p!=NULL)
strcpy(p,s);
returnp;
}
其中,其它的关于union、enum这里就不多说了,再说一个关于结构体的非常重要的应用——位操作:
当然,我们知道,对于位操作,我们可通过#definetables(即用宏和C中的位操作来实现)
如:
#defineKEYWORD01/*0001*/ #defineEXTERNAL02/*0010*/ #defineSTATIC04/*0100*/
或
enum{KEYWORD=01,EXTERNAL=02,STATIC=04};
那么,flags|=EXTERNAL|STATIC;将打开flags的EXTERNAL和STATIC位,而
flags&=~(EXTERNAL|STATIC);将关闭flags的EXTERNAL和STATIC位.
然而,上述定义的位模式可以用结构体如下写:
struct{
unsignedintis_keyword:1;
unsignedintis_extern:1;
unsignedintis_static:1;
}flags;/*Thisdefinesavariablecalledflagsthatcontainsthree1-bitfields*/
那么,上述打开相应位的操作为:
flags.is_extern=flags.is_static=1;
上述关闭相应位的操作为:
flags.is_extern=flags.is_static=0;