Preliminary Analysis of the Structure in C language programming, analysis of the C language structure

Source: Internet
Author: User

Preliminary Analysis of the Structure in C language programming, analysis of the C language structure

The C-language struct is one of the powerful functions of C and a favorable condition for C ++ language derivation. In fact, when the struct contains function pointers, struct is also the class in C ++.

In C language, struct declaration and definition use the keyword struct, just as the union uses the keyword union and the enum keyword for enumeration. In fact, the usage of consortium and enumeration types is almost Based on struct. The struct declaration format is as follows:

struct tag-name{{member 1;…member N;};

Therefore, the statement defining the struct variable is: struct tag-name varible-name, for example, struct point pt. Where, point is tag-name, and pt is the struct point variable. Of course, you can also declare the struct type and variable at one time, that is, the following: struct tag-name {...} X, y, z; is similar to int x, y, z. You can also assign the initial value when defining the struct variable, that is, the variable initialization, struct point pt = {320,200 };

Of course, there can be struct pointers and struct arrays. Methods To access member in struct variables include: If it is accessed by struct variable name, it is a structure-variable-name.member; if it is accessed by struct variable pointer, it is structure-variable-pointer-> member;

Well, the above is not the focus, it is not difficult to grasp, but the details. Struct has important applications:

For example, a self-referenced struct is often used as an important data structure such as a binary tree. Suppose we want to implement a general problem-solving algorithm-count the frequency of each word in some input. Because the number of input words is unknown, the content is unknown, and the length is unknown, we cannot sort the input and use binary search ....... Then, one solution is to sort known words by sorting each word to the appropriate position. Of course, this function cannot be achieved through linear sorting, because it may be very long. Correspondingly, we will use a binary tree. Each word of the Binary Tree is a binary tree node, and 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 a program, namely:

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

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

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

Here, we will not talk much about union and enum. Let's talk about a very important application about struct-bit operation:

Of course, we know that for bitwise operations, we can use # define tables (that is, using bitwise operations in macro and C)

For example:

#define KEYWORD 01 /*0001*/#define EXTERNAL 02 /*0010*/#define STATIC 04   /*0100*/

Or

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

Then, flags | = EXTERNAL | STATIC; will open the EXTERNAL and STATIC bits of flags, while

Flags & = ~ (EXTERNAL | STATIC); the EXTERNAL and STATIC bits of flags will be disabled.

However, the bitwise mode defined above can be written as follows using struct:

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

The following operations are performed to enable the corresponding bits:

flags.is_extern=flags.is_static=1;

The following operations are performed to disable the corresponding bits:

flags.is_extern=flags.is_static=0;

Articles you may be interested in:
  • Getting started with struct in C Language
  • Initialization of struct in C Language
  • In-depth analysis of the definition and reference of struct pointers in C Language

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.