If pointers are so problematic, why use them at all? First and foremost, pointers are not the problem; misuse of pointers is the problem.
Nonetheless, there are four main uses for pointers:
- To overcome thecall-by-valuerestriction in function parameters: Pointers expand the flexibility of function calls by allowing variable function parameters.
- As an alternative to array subscripting: Pointers allow access to array elements without subscripting.
- To manage C strings: Pointers allow easy (ahem, easier) allocation and manipulation of C strings.
- For dynamic data structures: Pointers allow memory to be allocated at runtime for useful dynamic structures, such as linked lists, trees, and dynamically sized arrays.
We will deal with the first point, the mechanics of pointers and variable function parameters, in this chapter. The second point will be explored in Chapter 14, Understanding Arrays...