Pointer Arrays and Array Pointers in C Explained with Examples
In C, a pointer array is an array whose elements are pointers to other objects. Each slot stores a memory adress, which makes this strcuture ideal for hendling variable‑length strings or collections of dynamically allocated data.
The following example defines a small array of integer pointers and a string pointer array. Elements are dereferenced and printed inside loops.
#include <stdio.h>
int main(void) {
int x = 11, y = 22, z = 33;
int *ptr_arr[] = {&x, &y, &z};
const char *words[] = {"alpha", "beta", "gamma", "delta"};
for (int idx = 0; idx < 2; idx++) {
printf("ptr_arr[%d] -> %d\n", idx, *ptr_arr[idx]);
}
for (int k = 0; k < 4; k++) {
printf("words[%d] = %s\n", k, words[k]);
}
return 0;
}
Output:
ptr_arr[0] -> 11
ptr_arr[1] -> 22
words[0] = alpha
words[1] = beta
words[2] = gamma
words[3] = delta
An array pointer points to an entire array rather than to a single element. It is commonly used with multi‑dimensional arrays or when an entire array needs to be passed to a function.
The code below creates a 3×4 integer matrix and a pointer that can step through whole rows. Two equivalent dereferencing styles are shown.
#include <stdio.h>
int main(void) {
int grid[3][4] = {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
int (*row_ptr)[4] = grid;
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 4; c++) {
// both lines produce identical output
printf("grid[%d][%d]: %d\n", r, c, *(row_ptr[r] + c));
// printf("grid[%d][%d]: %d\n", r, c, row_ptr[r][c]);
}
}
return 0;
}
Output:
grid[0][0]: 1
grid[0][1]: 2
grid[0][2]: 3
grid[0][3]: 4
grid[1][0]: 5
grid[1][1]: 6
grid[1][2]: 7
grid[1][3]: 8
grid[2][0]: 9
grid[2][1]: 10
grid[2][2]: 11
grid[2][3]: 12
Pointer arrays hold multiple addresses that may point to scattered13