- Tutorial on Advanced Pointers
The process() function takes the input buffer and a pointer to a pointer for the list! Personally I always find more than one *, eg ** confusing to work with. This is why I prefer to use typedefs like string or wordrecptr to minimize the number of *.
Why do I use a pointer to a pointer? Simply because the list starts empty and doesn't exist until the first word is added in the addword() function. To return the value of list from this function I had to make it a pointer! This is quite confusing- conceptually there is no list, just a pointer to the first struct. The value of this pointer is what we want to return to the caller of the function, so we need a pointer to it.
The for loop in process() uses the isalpha() function to extract characters and build up words. Anything non alphabetic ends a word and sets the inword flag to false and calls the addword() function. As soon as an alphabetic char is found it starts a new word. The function addword() does much of the work. It uses strcpy() to compare the word being added with every word in the list until it finds it or hits the end of the list.
This is a somewhat inefficient way of searching the list but it does ok. If it locates the word, it increments the count. The prev pointer variable points one structure behind the head, so that when the list is searched, when head hits the end, prev points to the last element. With the first word added, there is no list so the first structure is created and the list pointed to it.
On the next page : Summary of Other str functions

