Arrays can be passed to functions so their elements can be processed or updated directly. Since the function operates on the original array, changes made inside the function are reflected outside as well.
- Enables functions to access and modify the original array.
- Commonly used to process multiple elements efficiently.
Array Decay
When an array is passed to a function, it automatically decays into a pointer to its first element. As a result, the function receives the address of the first element rather than a copy of the entire array.
- Arrays cannot be passed by value in C.
- The function operates on the original array.
- The array size should usually be passed as a separate parameter.
Passing an Array Without Specifying the Size
The most common way is to declare the function parameter as an array without specifying its size.
#include <stdio.h>
void printArr(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
// Pass array to function
printArr(arr, 5);
return 0;
}
Output
1 2 3 4 5
Explanation: The array decays to a pointer when passed to printArr(). The size is passed separately so the function knows how many elements to process.
Passing an Array With a Fixed Size
You can also specify the array size in the parameter list. However, it is still treated as a pointer inside the function.
#include <stdio.h>
void printArr(int arr[5], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
// Pass array to function
printArr(arr, 5);
return 0;
}
Output
1 2 3 4 5
Explanation: Specifying the size in the parameter improves readability, but it does not change the way the array is passed.
Passing an Array Using Pointer Notation
Since an array decays into a pointer, the function parameter can also be declared as a pointer.
#include <stdio.h>
// Array passed as an array with the size of its
// dimension
void printArr(int* arr, int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
// Pass array to function
printArr(arr, 5);
return 0;
}
Output
1 2 3 4 5
Explanation: The pointer arr points to the first element of the array, allowing the function to access all elements using pointer or array indexing.
Passing Multidimensional Arrays
Multidimensional arrays can also be passed to functions. Unlike one-dimensional arrays, the compiler must know the size of all dimensions except the first so it can correctly locate each element.
Example:
void printMatrix(int arr[][3], int rows){
// Access matrix elements
}
- The first dimension can be omitted in the function parameter.
- All remaining dimensions must be specified.
- The array still decays to a pointer when passed to the function.
Best Practices
Follow these practices to make array handling in functions safer, more readable, and less error-prone.
- Pass the array size as a separate parameter.
- Use int *arr or int arr[] based on readability.
- Specify the remaining dimensions when passing multidimensional arrays.
- Use const if the function only reads the array.
Related article: Passing a 2D array as a parameter in C