奶头挺立呻吟高潮av全片,成人试看120秒体验区,性欧美极品v,A片高潮抽搐揉捏奶头视频

C語言

初步剖析C語言編程中的結構體

時間:2025-02-18 02:15:45 C語言 我要投稿
  • 相關推薦

初步剖析C語言編程中的結構體

  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#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]:get); }

  void ungetch(int c)/*push back character on input*/ {

  if(bufp>=BUFSIZE)

  printf("ungetch:too many charactersn");

  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=';

  return c;

  } for(;--lim>0;w++)

  if(!isalnum(*w=getch())){

  ungetch(*w);

  break;

  } *w=';

  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="" 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 %sn",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 '*/

  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;

【初步剖析C語言編程中的結構體】相關文章:

講解C語言編程中的結構體對齊09-14

C語言結構體中的函數指針06-14

C語言結構體定義06-25

C語言結構體教程06-17

C編程語言概述10-20

C語言編程基礎08-17

C語言的結構10-14

C語言結構體(struct)常見使用方法09-15

C語言復習之結構體基礎知識07-16

主站蜘蛛池模板: 乌什县| 建昌县| 伊金霍洛旗| 湘乡市| 陈巴尔虎旗| 衢州市| 洛隆县| 通河县| 桂东县| 永宁县| 新源县| 家居| 庆安县| 桂阳县| 新丰县| 贡觉县| 佛坪县| 汽车| 天津市| 晋城| 桐乡市| 龙泉市| 金寨县| 隆尧县| 忻州市| 静乐县| 屏东市| 恭城| 博乐市| 天柱县| 西充县| 武陟县| 肇庆市| 株洲县| 新沂市| 无棣县| 洪泽县| 抚宁县| 贵德县| 兴义市| 嵊州市|