Why Pass Arrays to Functions?
You often want to pass an array to a function so that you can:
π Process or modify its elements (like sum, sort, reverse)
π§± Break logic into reusable blocks
π§ Avoid rewriting the same logic multiple times
β οΈ IMPORTANT CONCEPT
In C, arrays are always passed to functions by reference.
This means:
You do not copy the entire array, just pass the memory address of the first element.
β So, any modification inside the function reflects in the original array.
β Syntax for Passing Arrays
π Function Declaration
void myFunction(int arr[], int size);or
void myFunction(int *arr, int size); // Equivalentint arr[] and int *arr mean the same here β a pointer to the first element.
π§ Basic Example β Print Array Elements
#include <stdio.h>
void printArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int length = sizeof(numbers)/sizeof(numbers[0]);
printArray(numbers, length);
return 0;
}1 2 3 4 5π Modify Array Elements Inside Function
This C program demonstrates passing an array to a function and modifying its contents using call-by-reference (via pointer semantics).
void doubleElements(int arr[], int size) {
for(int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
int main() {
int data[] = {5, 10, 15};
doubleElements(data, 3);
for(int i = 0; i < 3; i++) {
printf("%d ", data[i]); // Output: 10 20 30
}
return 0;
}10 20 30β What it does:
It takes two parameters:
arr[]: an array of integers (which is actually passed as a pointer in C)size: number of elements in the array
It doubles every element in the array using a loop:
arr[i] *= 2;is shorthand forarr[i] = arr[i] * 2;
π§ What happened?
arr[i] *= 2;inside the function directly modifies the original arraydata[].π― Important: Because arrays are passed by reference, not by value.
π― Key Point: Arrays Don't Need to Be Returned
#include <stdio.h>
void modifyArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
arr[i] = arr[i] * 10; // Multiply each element by 10
}
}
int main() {
int arr[] = {1, 2, 3}; // Original array
int size = 3;
// Pass array to function
modifyArray(arr, size);
// Print modified array
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}10 20 30Unlike int or float, you donβt need to return the array from the function β changes persist automatically.
π¦ Passing 2D Arrays to Functions
π Syntax:
void printMatrix(int arr[2][3]) {
// or int arr[][3]
}β You must specify column size in 2D arrays
void printMatrix(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}};
printMatrix(matrix, 2);
return 0;
}1 2 3
4 5 6Instead of arrays, you can also use pointer notation:
void printArray(int *arr, int size);You access elements like arr[i], which works the same as with array notation.
β Common Mistakes to Avoid
Mistake | Why itβs wrong |
|---|---|
Forgetting array size | The function wonβt know where array ends |
Modifying without intention | Changes reflect in original array |
Using | Not needed β arrays are already modified |
π§ Behind the Scenes: What's Really Happening?
When you pass an array:
int arr[] = {1, 2, 3};
func(arr, 3);C actually does this:
func(&arr[0], 3); // address of first elementThe function receives a pointer to the first element β not the whole array.