Preliminary analysis of structure in C language programming

Source: Internet
Author: User
C language structure is one of C's powerful functions, but also the C + + language can be derived from the favorable conditions, in fact, when the structure of the member has a function pointer, then, the struct is the class in C + +.

In c language, struct declarations, definitions are used in the keyword struct, just like the union used to the keyword union, enum type with the enum keyword, in fact, the use of Union, enumeration type is almost reference to the structure of the body. The declaration format for the struct is as follows:

struct Tag-name{{member 1;...member N;};

Therefore, the statement that defines a struct variable is: struct Tag-name varible-name, such as Struct Point pt, where point is tag-name,pt is struct struct a point variable. Of course, you can also declare struct types and variables at once, as follows: struct Tag-name {...} x, Y, z, x, y, Z; You can also assign an initial value when defining a struct variable, that is, variable initialization, struct point pt={320,200};

Of course, it is possible to have a struct pointer, an array of struct-bodies. The method of accessing member in a struct variable is structure-variable-name.member if it is accessed by the struct variable name, or if it is accessed by a struct variable pointer. It's structure-variable-pointer->member;.

Well, the above is not the focus, it is not difficult to grasp, but the details of the problem. Structures have important applications, such as the following:

such as self-referencing structure, commonly used as a binary tree and other important data structure implementation: Suppose we want to achieve a common problem of the solution algorithm-statistics of some input the frequency of the occurrence of each word. Since the number of words entered is unknown, the content is unknown, the length is unknown, we cannot sort the input and use binary search. ...... One solution, then, is to sort the known words-by sorting each arriving word into place. Of course, implementing this feature cannot be ordered linearly, because that might be long, and accordingly, we will use a two-fork tree to implement it. The two fork tree each word is a binary tree node, each node includes:

    • 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

It is written in the program, namely:

struct tnode{/*the tree Node:*/char *word;/*points to the Next*/int count;/*number of Occurences*/struct tnode *left;/*lef T child*/struct tnode *right;/*right child*/}

The complete procedure for completing the above functions is as follows:

#include
 
  #include
  
   #include
   
    
 #include "tNode.h" #define Maxword 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 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= ' + ';   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 sub   tree*/P->left=addtree (P-&GT;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 ' strcpy ' */if (p!=null) (p,s); return p; } 

     
    
   
  
 

Among them, the other about union, Enum here is not much to say, and again a very important application of the structure-bit operation:

Of course, we know that for bit operations, we can do this with a # define tables (that is, with a bit operation in macros and C)

Such as:

#define KEYWORD/*0001*/#define EXTERNAL/*0010*/#define STATIC   /*0100*/

Or

Enum{keyword =01,external =02,static = 04};

So, flags|=external| The external and static bits of the flags will be opened, and

Flags&=~ (external| static); The external and static bits of the flags will be closed.

However, the bit patterns defined above can be written in the following structure:

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

Then, the operation to open the corresponding bit is:

Flags.is_extern=flags.is_static=1;

The actions for closing the corresponding bits are:

flags.is_extern=flags.is_static=0;
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.