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.

In C, an array parameter is a function parameter declared to accept an array, but due to a language mechanic known as array decay, it does not receive a copy of the array. Instead, the compiler automatically converts the array parameter into a pointer to the array’s first element. Consequently, the function operates directly on the original memory addresses rather than a local copy.

Syntax Equivalence

When declaring a function, the compiler treats array notation and pointer notation in the parameter list as strictly identical. The specified size in the brackets is entirely ignored by the compiler.
// The compiler interprets all three signatures as: void process(int *arr)
void process(int arr[]);
void process(int arr[10]);
void process(int *arr);

Memory and the sizeof Illusion

Because the array decays into a pointer, type information regarding the array’s total size is lost across the function boundary. Applying the sizeof operator to an array parameter yields the size of the pointer type (typically 4 or 8 bytes), not the size of the original array.
void evaluate(int arr[]) {
    // This calculates (sizeof(int *) / sizeof(int)), which is incorrect for array length
    size_t length = sizeof(arr) / sizeof(arr[0]); 
}
To resolve this, the array’s length must be explicitly passed as an adjacent parameter:
void evaluate(int arr[], size_t length);

Multidimensional Array Parameters

Array decay only applies to the first dimension (the outermost bound) of an array. When passing multidimensional arrays, all subsequent dimensions must be explicitly defined in the parameter list. The compiler requires these inner bounds to calculate the correct memory offsets (pointer arithmetic) for element access.
// The first dimension decays, but the second dimension (5) is mandatory
void processMatrix(int matrix[][5], size_t rows);

// This is the exact pointer equivalent: a pointer to an array of 5 integers
void processMatrix(int (*matrix)[5], size_t rows);

Mutability and the const Qualifier

Because array parameters are implicitly pointers to the original data, any modifications made within the function alter the original array. To enforce read-only semantics and prevent unintended side effects, the const qualifier must be applied to the underlying type.
// The elements pointed to by 'arr' cannot be modified
void inspect(const int arr[], size_t length);

C99 static Array Indices

Introduced in C99, the static keyword can be placed inside the brackets of an array parameter. This does not change the decay-to-pointer behavior, but it serves as an optimization hint to the compiler, guaranteeing that the passed pointer will never be NULL and will point to an array containing at least the specified number of elements.
// Guarantees 'arr' is non-null and has at least 16 elements
void optimizedProcess(int arr[static 16]);
Master C with Deep Grasping Methodology!Learn More