原 C语言之强大的结构体,初步剖析C语言编程中的结构体

struct tag-name{

{

member 1;

member N;

};

因此,定义结构体变量的语句为:struct tag-name varible-name,如struct point pt;其中,point 为tag-name,pt是结构体struct point变量。当然,也可以一次性声明结构体类型和变量,即如下:struct tag-name {…} x,y,z;就类似于int x,y,z;语句一样。也可以在定义结构体变量时即赋初值,即变量初始化,struct point pt={320,200};

当然,也就可以有结构体指针、结构体数组了。访问结构体变量中的member的方法有:如果是由结构体变量名来访问,则是structure-variable-name.member;如果是由结构体变量指针来访问,则是structure-variable-pointer->member;

好了,上面的不是重点,也不难掌握,只是细节问题。结构体具有重要的应用,如下的:

如自引用的结构体,常用来作为二叉树等重要数据结构的实现:假设我们要实现一个普遍的问题的解决算法——统计某些输入的各单词出现的频数。由于输入的单词数是未知,内容未知,长度未知,我们不能对输入进行排序并采用二分查找。……那么,一种解决办法是:将已知的单词排序——通过将每个到达的单词排序到适当位置。当然,实现此功能不能通过线性排序,因为那样有可能很长,相应地,我们将使用二叉树来实现。该二叉树每一个单词为一个二叉树结点,每个结点包括:

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

其写在程序中,即:

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#include#include#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/*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;

}

其中,其它的关于union、enum这里就不多说了,再说一个关于结构体的非常重要的应用——位操作:

当然,我们知道,对于位操作,我们可通过#define tables(即用宏和C中的位操作来实现)

如:

#define KEYWORD 01 /*0001*/

#define EXTERNAL 02 /*0010*/

#define STATIC 04 /*0100*/

enum{KEYWORD =01,EXTERNAL =02,STATIC =04};

那么,flags|=EXTERNAL|STATIC;将打开flags的EXTERNAL和STATIC位,而

flags&=~(EXTERNAL|STATIC);将关闭flags的EXTERNAL和STATIC位.

然而,上述定义的位模式可以用结构体如下写:

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*/

那么,上述打开相应位的操作为:

网友评论

文明上网理性发言,请遵守 新闻评论服务协议我要评论

47d507a036d4dd65488c445c0974b649.png

立即提交

专题推荐064df72cb40df78e80e61b7041ee044f.png独孤九贱-php全栈开发教程

全栈 100W+

主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门

7dafe36c040e31d783922649aefe0be1.png玉女心经-web前端开发教程

入门 50W+

主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门

04246fdfe8958426b043c89ded0857f1.png天龙八部-实战开发教程

实战 80W+

主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习

php中文网:公益在线php培训,帮助PHP学习者快速成长!

Copyright 2014-2021 https://www.php.cn/ All Rights Reserved | 苏ICP备2020058653号-1e6cebb680dfe320dad7e62bd6442c3a6.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值