初步剖析C语言编程中的结构体


Posted in Python onJanuary 16, 2016

C语言结构体,可谓是C强大功能之一,也是C++语言之所以能衍生的有利条件,事实上,当结构体中成员中有函数指针了后,那么,结构体也即C++中的类了。

C语言中,结构体的声明、定义是用到关键字struct,就像联合体用到关键字union、枚举类型用到enum关键字一样,事实上,联合体、枚举类型的用法几乎是参照结构体来的。结构体的声明格式如下:

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<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; 
}

其中,其它的关于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*/

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

flags.is_extern=flags.is_static=1;

上述关闭相应位的操作为:

flags.is_extern=flags.is_static=0;
Python 相关文章推荐
Python程序设计入门(3)数组的使用
Jun 16 Python
Python获取DLL和EXE文件版本号的方法
Mar 10 Python
python通过urllib2获取带有中文参数url内容的方法
Mar 13 Python
举例讲解Python中的迭代器、生成器与列表解析用法
Mar 20 Python
详解Python中contextlib上下文管理模块的用法
Jun 28 Python
浅谈django三种缓存模式的使用及注意点
Sep 30 Python
解决django前后端分离csrf验证的问题
Feb 03 Python
Python 单例设计模式用法实例分析
Sep 23 Python
python调用HEG工具批量处理MODIS数据的方法及注意事项
Feb 18 Python
Python SMTP配置参数并发送邮件
Jun 16 Python
详解Open Folder as PyCharm Project怎么添加的方法
Dec 29 Python
Pycharm 设置默认解释器路径和编码格式的操作
Feb 05 Python
举例讲解Python设计模式编程的代理模式与抽象工厂模式
Jan 16 #Python
python实现发送和获取手机短信验证码
Jan 15 #Python
详解python单例模式与metaclass
Jan 15 #Python
理解python正则表达式
Jan 15 #Python
Python工程师面试题 与Python Web相关
Jan 14 #Python
Python工程师面试题 与Python基础语法相关
Jan 14 #Python
5种Python单例模式的实现方式
Jan 14 #Python
You might like
php实现的简单日志写入函数
2015/03/31 PHP
通过修改配置真正解决php文件上传大小限制问题(nginx+php)
2015/09/23 PHP
浅谈laravel框架sql中groupBy之后排序的问题
2019/10/17 PHP
如何实现浏览器上的右键菜单
2006/07/10 Javascript
JavaScript高级程序设计
2006/12/29 Javascript
jQuery DOM操作小结与实例
2010/01/07 Javascript
表单JS弹出填写提示效果代码
2011/04/16 Javascript
JavaScript获取FCK编辑器信息的具体方法
2013/07/12 Javascript
JavaScript定时器详解及实例
2013/08/01 Javascript
取消选中单选框radio的三种方式示例介绍
2013/12/23 Javascript
解决自定义$(id)的方法与jquery选择器$冲突的问题
2014/06/14 Javascript
JavaScript正则表达式的分组匹配详解
2016/02/13 Javascript
jquery实现网页定位导航
2016/08/23 Javascript
解决浏览器会自动填充密码的问题
2017/04/28 Javascript
JavaScript之面向对象_动力节点Java学院整理
2017/06/29 Javascript
inner join 内联与left join 左联的实例代码
2017/09/18 Javascript
微信小程序wx.uploadfile 本地文件转base64的实现代码
2018/06/28 Javascript
vuejs选中当前样式active的实例
2018/08/22 Javascript
从零到一详聊创建Vue工程及遇到的常见问题
2019/04/25 Javascript
解决vant中 tab栏遇到的坑 van-tabs
2020/11/04 Javascript
使用python实现strcmp函数功能示例
2014/03/25 Python
Python matplotlib画图实例之绘制拥有彩条的图表
2017/12/28 Python
python用plt画图时,cmp设置方法
2018/12/13 Python
基于Python对数据shape的常见操作详解
2018/12/25 Python
程序员的七夕用30行代码让Python化身表白神器
2019/08/07 Python
Django自带的用户验证系统实现
2020/12/18 Python
Petmate品牌官方网站:宠物用品
2018/11/25 全球购物
给排水专业应届生求职信
2013/10/12 职场文书
舞蹈教育学专业推荐信
2013/11/27 职场文书
工作中个人的自我评价
2013/12/31 职场文书
家庭教育先进个人事迹材料
2014/01/24 职场文书
2014年母亲节演讲稿范文
2014/05/07 职场文书
解除财产保全担保书
2014/05/20 职场文书
个人承诺书格式
2014/06/03 职场文书
中学生旷课检讨书2篇
2014/10/09 职场文书
Java版 简易五子棋小游戏
2022/05/04 Java/Android