276 Chapter 11 Pointers
As you can see from this chapter, the pointer is a very powerful construct in C.The
flexibility in defining pointer variables extends beyond those illustrated in this chapter.
For example, you can define a pointer to a pointer, and even a pointer to a pointer to a
pointer.These types of constructs are beyond the scope of this book, although they are
simply logical extensions of everything you’ve learned about pointers in this chapter.
The topic of pointers is probably the hardest for novices to grasp.You should reread
any sections of this chapter that still seem unclear before proceeding. Solving the exercis-
es that follow will also help you to understand the material.
Exercises
- Type in and run the 15 programs presented in this chapter. Compare the output
produced by each program with the output presented after each program in the
text. - Write a function called insertEntryto insert a new entry into a linked list. Have
the procedure take as arguments a pointer to the list entry to be inserted (of type
struct entryas defined in this chapter), and a pointer to an element in the list
afterwhich the new entryis to be inserted. - The function developed in exercise 2 only inserts an element after an existing ele-
ment in the list, thereby preventing you from inserting a new entry at the front of
the list. How can you use this same function and yet overcome this problem?
(Hint:Think about setting up a special structure to point to the beginning of the
list.) - Write a function called removeEntryto remove an entryfrom a linked list.The
sole argument to the procedure should be a pointer to the list. Have the function
remove the entry afterthe one pointed to by the argument. (Why can’t you
remove the entry pointed to by the argument?) You need to use the special struc-
ture you set up in exercise 3 to handle the special case of removing the first ele-
ment from the list. - A doubly linked listis a list in which each entry contains a pointer to the preceding
entry in the list as well as a pointer to the next entry in the list. Define the appro-
priate structure definition for a doubly linked list entry and then write a small pro-
gram that implements a small doubly linked list and prints out the elements of the
list. - Develop insertEntryand removeEntryfunctions for a doubly linked list that are
similar in function to those developed in previous exercises for a singly linked list.
Why can your removeEntryfunction now take as its argument a direct pointer to
the entry to be removed from the list? - Write a pointer version of the sortfunction from Chapter 8, “Working with
Functions.” Be certain that pointers are exclusively used by the function, including
index variables in the loops.