Arrays of pointers vs Pointers to arrays
What you'll learn: How to distinguish between a collection of memory addresses and a single pointer that tracks an entire array structure.
The Power of Parentheses
In C, the square brackets [] have a higher precedence than the asterisk *. This tiny detail completely changes how the compiler interprets your data structures. When we talk about "Arrays of Pointers" versus "Pointers to Arrays," we are really talking about where you put the parentheses.
1. Arrays of Pointers: The Address Book
An array of pointers is exactly what it sounds like: a list where every "slot" holds a memory address.
Imagine a row of mailboxes. Inside each mailbox is a slip of paper with the address of a house. The mailboxes themselves don't hold the houses; they just hold the directions to them.
int *ptr_array[5];
Here, ptr_array is an array of 5 elements. Each element is an int * (a pointer to an integer). This is commonly used for handling lists of strings or creating "jagged arrays" where each row can have a different length.
2. Pointers to Arrays: The Master Blueprint
A pointer to an array is a single pointer variable that points to a whole block of memory formatted as an array.
Imagine you have a specific architectural blueprint for a 5-room house. A pointer to an array is like a single finger pointing at that entire blueprint. It doesn't point to one room; it points to the "5-room structure" as a whole.
int (*array_ptr)[5];
By adding parentheses (*array_ptr), we force the compiler to treat array_ptr as a pointer first. The [5] tells the compiler that the destination is an array of five integers. If you increment this pointer (array_ptr++), it jumps forward by the size of five integers at once, rather than just one.
The Visual Difference
To keep them straight, look at the "core" of the declaration:
int *arr[5]-> The[5]wins. It is an Array of pointers.int (*arr)[5]-> The(*)wins. It is a Pointer to an array.
Key takeaway
int *a[n] is a collection of many addresses, while int (*a)[n] is a single address pointing to a specific array structure.