Array of Pointers
Array of Pointers in C
An array of pointers is simply an array that stores addresses, rather than actual values.
Declaration
Like a regular array, an array of pointers can be declared easily:
Assigning Addresses
We can make the pointers point to integer variables:
Here, p[0], p[1], and p[2] point to the variables a, b, and c respectively.
Advantages
-
An array of pointers can point to an array of data items.
-
Pointers can be reordered without moving the actual data items.
-
Reordering pointers is much faster than reordering large data items such as records or strings.
-
For example, sorting an array of strings is easier by reordering pointers rather than swapping the strings themselves.
Example: Sorting Names Using Array of Pointers
How It Works
-
name[100][50]stores the actual strings. -
ptr[100]stores pointers to the strings. -
Sorting is done by swapping pointers, not the strings themselves.
-
This approach is memory-efficient and faster for large data.
Comments
Post a Comment