Page 113 - Programming Microcontrollers in C
P. 113
98 Chapter 2 Advanced C Topics
/* word frequency count */
#define MAXWORD 100
int main(void)
{
Tnode *root;
char word[MAXWORD];
int i;
root=NULL;
while(getword(word,MAXWORD) != EOF)
if(isalpha(word[0]))
root=addtree(root,word);
treeprint(root);
return 0;
}
A NULL value is assigned to root, and the program enters a loop that
reads words from the input stream. It is assumed that if the first character
of the word is a letter, that a word has been read in. The function
isaplha() returns a TRUE if its argument is a letter, and a FALSE if
it is not. If the input is a letter, the routine addtree is executed with the
arguments root and word. The first time that this function is executed,
root is a NULL. This loop is repeatedly executed until getword()
receives an EOF character from the input stream. At that time the input
loop terminates, and the function treeprint() is executed. When
treeprint() is completed, main() returns a 0 to the calling pro
gram or the operating system. This signal can be used to notify the
operating system that the program has executed correctly.
Here is a good point to introduce the concept of a NULL pointer.
Often people use the term NULL to mean zero or a character contain
ing a zero. This idea is incorrect. In this text, the word NULL means
a NULL pointer to a type void. Such a pointer can be defined in a
header file and the definition will look like
#define NULL ((* void) 0)
Any place you see the name NULL in this text, it has the above meaning.
The function addtree() is the most complicated function in