Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A pointer to an array is a distinct derived data type in C that stores the memory address of an entire array, rather than pointing to a single element. It encapsulates both the base data type and the dimension of the array within its type signature. While typically associated with arrays of fixed compile-time sizes, C99 introduced Variable Length Arrays (VLAs), allowing pointers to arrays where the dimension is evaluated at runtime.

Syntax and Declaration

Because the array subscript operator [] has higher precedence than the dereference operator *, parentheses are mandatory when declaring a pointer to an array.
// Pointer to a fixed-size array of 5 integers
int (*pointer_name)[5];

// Pointer to a Variable Length Array of 'n' integers (C99)
int n = 5;
int (*vla_pointer_name)[n];
This must be strictly distinguished from an array of pointers, which omits the parentheses:
int *ptr_array[5]; // Array of 5 pointers to integers

Initialization and Type Compatibility

When an array identifier is used in an expression, it typically decays into a pointer to its first element. However, when the address-of operator (&) is applied to an array identifier, it yields the address of the entire array.
int arr[5] = {10, 20, 30, 40, 50};

// Correct: &arr is of type 'int (*)[5]'
int (*ptr)[5] = &arr; 

// Incorrect: arr decays to 'int *', causing a type mismatch
// int (*ptr)[5] = arr; 
While arr and &arr evaluate to the same memory address at runtime, their compile-time types and behaviors are fundamentally different:
  • arr has the compile-time type int[5]. In most expressions (with notable exceptions like sizeof(arr) and &arr), it decays into a pointer to its first element, yielding the type int *.
  • &arr applies the address-of operator directly to the array type, yielding a pointer to the entire array. Its type is int (*)[5].

Pointer Arithmetic

Pointer arithmetic is dictated by the size of the type being pointed to. Incrementing a pointer to an array advances the memory address by the total byte size of the entire array, not a single element.
int arr[5];
int (*ptr)[5] = &arr;

// Assuming sizeof(int) is 4 bytes:
// The size of the pointed-to type is 5 * 4 = 20 bytes.
// If ptr holds address 0x1000:
ptr + 1; // Evaluates to 0x1000 + 20 bytes = 0x1014

Dereferencing and Element Access

To access individual elements through a pointer to an array, you must first dereference the pointer to yield the array itself. Due to operator precedence, the dereference must be enclosed in parentheses before applying the subscript operator.
int arr[5] = {10, 20, 30, 40, 50};
int (*ptr)[5] = &arr;

// 1. *ptr dereferences the pointer, yielding the array 'arr' (type int[5]).
// 2. The array 'arr' then decays into a pointer to its first element (type int *).
// 3. [2] accesses the element at index 2.
int val = (*ptr)[2]; // val is 30
Alternatively, using pointer arithmetic syntax:
int val = *(*ptr + 2); // Equivalent to (*ptr)[2]

Relationship to Multi-Dimensional Arrays

In C’s type system, a 2D array is an array of 1D arrays. When a 2D array identifier decays, it becomes a pointer to its first element—which is a 1D array. Therefore, a pointer to an array is the exact type yielded by the decay of a 2D array.
int matrix[3][5];

// 'matrix' has type int[3][5].
// It decays to a pointer to its first element.
// The first element is an array of 5 integers (type int[5]).
// Therefore, the decayed type is 'int (*)[5]'.
int (*ptr)[5] = matrix; 

Function Parameters and Array Adjustment

The most critical application of pointers to arrays is passing multi-dimensional arrays to functions. When an array of arrays is declared as a function parameter, C automatically adjusts the parameter type to be a pointer to an array.
// These three function signatures are exactly equivalent:
void process_matrix(int (*matrix)[5]);
void process_matrix(int matrix[][5]);
void process_matrix(int matrix[3][5]); // The '3' is ignored by the compiler
Because a 2D array like int matrix[3][5] decays into a pointer to its first element (int (*)[5]) when passed as an argument, the receiving function must accept a pointer to an array of the correct inner dimension. This ensures that pointer arithmetic within the function correctly skips over entire rows (e.g., matrix[1] advances the pointer by 5 * sizeof(int) bytes).
Master C with Deep Grasping Methodology!Learn More