Chapter 6 K & R study notes

Source: Internet
Author: User

Chapter 6 describes the structure. It provides a method for programmers to construct their own complex types by combining existing basic types.
There are not many operations supported by the structure, including copying and assigning values, obtaining addresses, and accessing members. For copying and assigning values, you can assign values to a single member of a struct, or assign values to the entire Member. When the struct is large and the cost of copying is high, you can also use the pointer passing method.
For some struct operations, we must always remember that ".", "->", "()", and "[]" have the highest priority. Therefore, * P-> STR is a member of the P structure, and * P-> STR ++ is a self-incrementing STR after the STR structure is resolved. + P-> Len is the auto-increment of Len.

A typical example of data structure is binary tree:

# Include <stdio. h> # include <ctype. h> # include <string. h> # include <stdlib. h> # define maxword100 # define bufsize 100 // buffer size char Buf [bufsize]; // buffer int bufp = 0; // buffer index // Binary Tree struct tnode {char * word; int count; struct tnode * left; struct tnode * Right ;}; // note, an instance containing its own structure is invalid, but both left and right point to the tnode type pointer, rather than the instance struct tnode * addtree (struct tnode *, char *); void treeprint (struct tnode *); int getword (char *, INT); struct Tnode * talloc (void); char * strdup (char *); struct tnode * talloc (); int main (void) {struct tnode * root; char word [maxword]; root = NULL; while (getword (word, maxword )! = EOF) {If (isalpha (word [0]) // according to word [0], we can see whether the string in Word is valid after getword is called. Root = addtree (root, word);} treeprint (Root); Return 0;} struct tnode * addtree (struct tnode * P, char * w) {int cond; If (P = NULL) {P = talloc (); P-> word = strdup (w); P-> COUNT = 1; p-> left = p-> right = NULL ;} else if (cond = strcmp (W, p-> word) = 0) // If W is equal to P's word {P-> count ++; // increase count} else if (cond <0) {P-> left = addtree (p-> left, W ); // compare the word} else {P-> of the Left subtree Right = addtree (p-> right, W); // compare the word of the right word} return P;} // print the binary tree void treeprint (struct tnode * P) {If (P! = NULL) {treeprint (p-> left); printf ("% 4D % s", p-> count, p-> word ); treeprint (p-> right) ;}// allocate a Binary Tree Memory and return its pointer struct tnode * talloc () {return (struct tnode *) malloc (sizeof (struct tnode);} // copy the string S and return it char * strdup (char * s) {char * P; P = (char *) malloc (sizeof (strlen (s) + 1); If (P! = NULL) strcpy (P, S); Return P;} // read a word from the input stream or buffer and fill it in the word, EOF, the first letter of a word, or other non-letter int getword (char * word, int Lim) {int C, getch (void); void ungetch (INT ); char * w = word; while (isspace (C = getch () // skip spaces; If (C! = EOF) * w ++ = C; If (! Isalpha (c) // {* w = '\ 0'; return C; // return the element that is not a character} For (; -- Lim> 0; W ++) {If (! Isalnum (* w = getch () // if it is not a letter or number, it indicates that the word has been read {ungetch (* w ); // press the element that is not in the stack into break;} * w = '\ 0'; return word [0]; // return the first character of the word, it indicates that the word is successfully filled in} // if there is content in the buffer, it will be read from the buffer; otherwise, it will be read from the input stream int getch (void) {return (bufp> 0 )? Buf [-- bufp]: getchar ();} // press a character into the buffer void ungetch (INT c) {If (bufp> = bufsize) printf ("ungetch: too character characters \ n "); elsebuf [bufp ++] = C ;}

It is not difficult to understand the data structure, but the getword function is designed strangely. Its function is to read words into word, but its return value is very interesting, or non-letters, so it is the first letter of word. The purpose of this design is:

While (getword (word, maxword )! = EOF) {If (isalpha (word [0]) // according to word [0], we can see whether the string in Word is valid after getword is called. Root = addtree (root, word );}

You only need to make one judgment to see if it is a letter. I personally think this method is not very readable, but it is better to return EOF,-1 (indicating not a letter), and 1 (indicating a letter ).

There is nothing to say about the hash table example.

struct nlist *install(char *name, char *defn){struct nlist *np;unsigned hashval;if ((np = lookup(name)) == NULL) {np = (struct nlist *) malloc(sizeof(*np));if (np == NULL || (np->name = strdup(name)) == NULL)return NULL;hashval = hash(name);np->next = hashtab[hashval];hashtab[hashval] = np;} else free((void *) np->defn); if ((np->defn = strdup(defn)) == NULL)return NULL;return np;}

Note the following two sentences:

np->next = hashtab[hashval];hashtab[hashval] = np;

The second inserted element is placed in front of the first inserted element. But I don't know why it should be designed in descending order.

Typedef is simple and can be used to simplify some complicated definitions. It is worth noting that if the data type declared by typedef is related to the machine, you only need to modify the content of typedef when porting from one machine to another, this greatly improves the portability of the program.
It is the first time that I have met the content of a bit field. Its role is similar to that of a mask, but it does not seem to be as widely used as a mask.

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.