Page 112 - Programming Microcontrollers in C
P. 112

Structures     97

                              The first two elements to this structure are a pointer to the word
                          contained in the node, and the number of times that the word has
                          been seen. The last two elements are pointers to structures of the
                          type struct tnode. Note that in the typedef of the struct
                          tnode, the structure tag was used. It is necessary to use the tag in
                          this case because the self-referential pointers inside of the structure
                          needs the tag to find the correct type. For the remainder of the pro­
                          gram, the typedef Tnode is used.
                              Some new files are included in the include files. These files will
                          be discussed in a following section. The first is ctype.h. This pro­
                          gram uses several character tests that are identified in ctype.h.
                          There are string operations found in string.h , and there are stan­
                          dard functions defined in stdlib.h.

                   #include <stdio.h>
                   #include <ctype.h>
                   #include <string.h>
                   #include <stdlib.h>

                              The list of function prototypes for the functions written in this
                          program follow:

                   Tnode *addtree(TNODE *, char *);
                   Tnode *talloc(void);
                   void treeprint( TNODE *);
                   int getword(char *, int);
                   char *strsave(char *);
                              The first function is the function that is used to add a tree to the
                          program. *talloc() is a function that allocates memory for a
                          Tnode. The function treeprint() prints out the tree, and
                          getword() reads in a word from the input stream. There are two
                          function prototypes defined within the program getword().These
                          functions are used by getword() only. The final function above
                          saves the string pointed to by the argument in a safe place and re­
                          turns a pointer to the word location in memory.
                              The main program is relatively simple. The constant MAXWORD
                          is the maximum number of characters that can be allowed in a word.
                          Within main() a pointer to a structure Tnode named root is de­
                          clared along with a character array named word and an integer i.
   107   108   109   110   111   112   113   114   115   116   117