Sams Teach Yourself C in 21 Days

(singke) #1
The program in Listing 15.7 is the most complex you have yet encountered in this book.
It uses many of the C programming techniques that were covered in previous lessons.
With the aid of the preceding explanation, you should be able to follow the program’s
operation and understand each step. If you find areas that are unclear to you, review the
related sections of this book until you understand.

Working with Pointers to Functions ..................................................................


Pointers to functions provide another way of calling functions. “Hold on,” you might be
thinking. “How can you have a pointer to a function? Doesn’t a pointer hold the address
where a variable is stored?”
Well, yes and no. It’s true that a pointer holds an address, but it doesn’t have to be the
address where a variable is stored. When your program runs, the code for each function
is loaded into memory starting at a specific address. A pointer to a function holds the
starting address of a function—its entry point.
Why use a pointer to a function? As I mentioned earlier, it provides a more flexible way
of calling a function. It lets the program choose from among several functions, selecting
the one that is appropriate for the current circumstances.

406 Day 15

FIGURE15.5
Before sorting, the
pointers are in the
same order in which
the strings were
entered.

FIGURE15.6
After sorting, the
pointers are ordered
according to the
alphabetical order of
the strings.

lines[0]
lines[1]
lines[2]
lines[3]
lines[4]

zoo\0

program\0zoo

applezo\0

dog\0

merrzy\0o

lines[0]
lines[1]
lines[2]
lines[3]
lines[4]

zoo\0

programzoo\0

applzoe\0

dog\0

merryzo\0

25 448201x-CH15 8/13/02 11:13 AM Page 406

Free download pdf