Next: Multidimensional arrays and pointers
Up: Pointers
Previous: Pointers and Arrays
We can have arrays of pointers since pointers are variables.
Example use:
Sort lines of text of different length.
NOTE: Text can't be moved or compared in a single operation.
Arrays of Pointers are a data representation that will cope efficiently
and conveniently with variable length text lines.
How can we do this?:
- Store lines end-to-end in one big char array
(Fig.
).
n will delimit lines.
- Store pointers in a different array where each pointer points to 1st char
of each new line.
- Compare two lines using strcmp() standard library function.
- If 2 lines are out of order - swap pointer in pointer array
(not text).
Fig.
Arrays of Pointers (String Sorting Example)
This eliminates:
- complicated storage management.
- high overheads of moving lines.