初步剖析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爬虫之打包生成exe文件
Nov 06 Python
Python获取linux主机ip的简单实现方法
Apr 18 Python
用python与文件进行交互的方法
Mar 01 Python
pandas将DataFrame的列变成行索引的方法
Apr 10 Python
Python反射和内置方法重写操作详解
Aug 27 Python
Python 实现输入任意多个数,并计算其平均值的例子
Jul 16 Python
用Pytorch训练CNN(数据集MNIST,使用GPU的方法)
Aug 19 Python
opencv-python 读取图像并转换颜色空间实例
Dec 09 Python
selenium+Chrome滑动验证码破解二(某某网站)
Dec 17 Python
python实现输入三角形边长自动作图求面积案例
Apr 12 Python
浅谈matplotlib 绘制梯度下降求解过程
Jul 12 Python
python线程优先级队列知识点总结
Feb 28 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+AJAX聊天程序[聊天室]提供下载
2007/07/21 PHP
php操作xml
2013/10/27 PHP
仅img元素创建后不添加到文档中会执行onload事件的解决方法
2011/07/31 Javascript
对Jquery中的ajax再封装,简化操作示例
2014/02/12 Javascript
js实现按钮加背景图片常用方法
2014/11/01 Javascript
jquery实现最简单的滑动菜单效果代码
2015/09/12 Javascript
Javascript编程之继承实例汇总
2015/11/28 Javascript
jQuery页面弹出框实现文件上传
2017/02/09 Javascript
JavaScript简单实现合并两个Json对象的方法示例
2017/10/16 Javascript
基于Vue的ajax公共方法(详解)
2018/01/20 Javascript
Vue中 v-if 和v-else-if页面加载出现闪现的问题及解决方法
2018/10/12 Javascript
Jquery遍历筛选数组的几种方法和遍历解析json对象,Map()方法详解以及数组中查询某值是否存在
2019/01/18 jQuery
webpack-url-loader 解决项目中图片打包路径问题
2019/02/15 Javascript
javascript数组的定义及操作实例
2019/11/10 Javascript
vue中使用腾讯云Im的示例
2020/10/23 Javascript
[01:23]一分钟告诉你 DOTA2为什么叫信仰2
2014/06/20 DOTA
Python 列表(List)操作方法详解
2014/03/11 Python
在Python中使用next()方法操作文件的教程
2015/05/24 Python
python 网络爬虫初级实现代码
2016/02/27 Python
python 顺时针打印矩阵的超简洁代码
2018/11/14 Python
selenium 安装与chromedriver安装的方法步骤
2019/06/12 Python
Python Threading 线程/互斥锁/死锁/GIL锁
2019/07/21 Python
对python中assert、isinstance的用法详解
2019/11/27 Python
Python基于Tensor FLow的图像处理操作详解
2020/01/15 Python
Python第三方包PrettyTable安装及用法解析
2020/07/08 Python
Python 实现集合Set的示例
2020/12/21 Python
利用CSS3实现开门效果实例源码
2016/08/22 HTML / CSS
HTML5之SVG 2D入门7—SVG元素的重用与引用
2013/01/30 HTML / CSS
解锁canvas导出图片跨域的N种姿势小结
2019/01/24 HTML / CSS
岗位竞聘书范文
2014/03/31 职场文书
小学生作文评语
2014/04/18 职场文书
遗失说明具结保证书
2015/02/26 职场文书
入党介绍人意见范文
2015/06/01 职场文书
整脏治乱工作简报
2015/07/21 职场文书
总结高并发下Nginx性能如何优化
2021/11/01 Servers
利用 Python 的 Pandas和 NumPy 库来清理数据
2022/04/13 Python