Pass an Array to a Function in C
In C, we can pass an array to a function by passing its base address. Since arrays are inherently passed by reference, any modifications inside the function affect the original array. This allows functions to operate efficiently on large datasets without copying the entire array.
Examples of Passing an Array to a Function
1. Passing an Array to a Function to Print Elements
In this example, we define a function that takes an integer array and its size as arguments. The function iterates through the array and prints each element.
main.c
#include <stdio.h>
// Function to print elements of an array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);
// Passing array to function
printArray(numbers, size);
return 0;
}
Explanation:
- We define the function
printArray()
that takes an integer arrayarr[]
and its size as parameters. - Inside
printArray()
, afor
loop iterates through the array and prints each element. - In
main()
, we declare an arraynumbers[]
and calculate its size usingsizeof(numbers) / sizeof(numbers[0])
. - We call
printArray()
, passingnumbers
andsize
. - The function prints the elements of the array.
Output:
1 2 3 4 5
2. Modifying an Array Inside a Function
In this example, we pass an array to a function that doubles each element. Since arrays are passed by reference, the modifications reflect in the original array.
main.c
#include <stdio.h>
// Function to double each element in the array
void doubleArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
int main() {
int numbers[] = {2, 4, 6, 8, 10};
int size = sizeof(numbers) / sizeof(numbers[0]);
// Passing array to function
doubleArray(numbers, size);
// Printing modified array
for (int i = 0; i < size; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Explanation:
- We define the function
doubleArray()
that takes an integer arrayarr[]
and its size. - A
for
loop iterates through the array, multiplying each element by 2. - In
main()
, we declare and initialize an arraynumbers[]
. - We call
doubleArray()
, passingnumbers
andsize
. - After returning, we print the modified array, showing that values have doubled.
Output:
4 8 12 16 20
3. Passing a Multi-Dimensional Array to a Function
In this example, we pass a 2D array to a function that prints its elements.
main.c
#include <stdio.h>
// Function to print a 2D array
void print2DArray(int arr[][3], int rows) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
int main() {
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Passing 2D array to function
print2DArray(matrix, 2);
return 0;
}
Explanation:
- We define the function
print2DArray()
that takes a 2D array with fixed column size3
and the number of rows. - Two nested
for
loops iterate through the rows and columns, printing elements. - In
main()
, we declare a 2D arraymatrix[][]
with 2 rows and 3 columns. - We call
print2DArray()
, passingmatrix
and row count2
.
Output:
1 2 3
4 5 6
Conclusion
In this tutorial, we explored different ways to pass arrays to functions in C:
- Passing a 1D array to print its elements.
- Modifying an array inside a function.
- Passing a 2D array to a function.
Understanding how to pass arrays to functions helps in creating reusable and efficient code.