How to Pass 1D & 2D Arrays to Functions in C – A Practical Guide
How to Pass 1D & 2D Arrays to Functions in C
This tutorial explains how to pass entire arrays—both one‑dimensional and multi‑dimensional—to functions in C, with clear code examples and best‑practice tips.
In C, an array name decays to a pointer when passed to a function, allowing the function to operate on the original data without copying it. Understanding this behavior is essential for efficient, maintainable code.
Passing Individual Array Elements
Passing a single element is identical to passing any primitive type.
Example 1: Pass Individual Array Elements
#include <stdio.h>
void display(int age1, int age2) {
printf("%d\n", age1);
printf("%d\n", age2);
}
int main() {
int ageArray[] = {2, 8, 4, 12};
// Pass second and third elements to display()
display(ageArray[1], ageArray[2]);
return 0;
}
Output
8 4
Here we pass ageArray[1] and ageArray[2] just like ordinary variables. The function receives them by value, so the original array remains unchanged.
Passing Entire One‑Dimensional Arrays
To hand an entire array to a function, simply supply the array name. The function prototype must indicate an array parameter, but the actual size can be omitted. The compiler treats the parameter as a pointer to the first element.
Example 2: Compute Sum of an Array
#include <stdio.h>
float calculateSum(float num[]);
int main() {
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};
// Pass the entire array to calculateSum()
result = calculateSum(num);
printf("Result = %.2f", result);
return 0;
}
float calculateSum(float num[]) {
float sum = 0.0;
for (int i = 0; i < 6; ++i) {
sum += num[i];
}
return sum;
}
Output
Result = 162.50
The function signature float calculateSum(float num[]) tells the compiler that num is a pointer to a float array. The loop processes the array in place without copying.
Passing Multi‑Dimensional Arrays
Multi‑dimensional arrays follow the same principle: the array name decays to a pointer to the first row. However, the compiler must know the size of every dimension except the first. This is required so it can calculate the correct memory offsets.
Example 3: Pass a Two‑Dimensional Array
#include <stdio.h>
void displayNumbers(int num[2][2]);
int main() {
int num[2][2];
printf("Enter 4 numbers:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
scanf("%d", &num[i][j]);
}
}
// Pass the two‑dimensional array to a function
displayNumbers(num);
return 0;
}
void displayNumbers(int num[2][2]) {
printf("Displaying:\n");
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
printf("%d\n", num[i][j]);
}
}
}
Output
Enter 4 numbers: 2 3 4 5 Displaying: 2 3 4 5
The function prototype void displayNumbers(int num[2][2]) declares a 2×2 integer array. When calling the function, we simply pass num; the compiler automatically handles the pointer conversion.
Note that you may omit the row dimension but must specify the column size: int num[][2] is valid, while the column size is essential for address calculation.
Important: C allows passing arrays to functions, but it does not support returning arrays from functions directly. Use pointers or structures if you need to return complex data.
C Language
- Mastering C# Arrays: Declaration, Initialization, and Operations
- Mastering C++ Functions: From Basics to Advanced Usage
- C++ Function Overloading: A Practical Guide
- Mastering C++ Arrays: Declaration, Initialization, and Practical Examples
- Mastering C Functions: User-Defined and Standard Library Basics
- Mastering User-Defined Functions in C: A Step‑by‑Step Guide
- Mastering C Arrays: Declaration, Initialization, and Common Operations
- Mastering C Functions: Structure, Declaration, and Best Practices
- C Arrays Explained: Efficient Data Storage & Access
- Mastering Arrays in C#: Efficient Data Storage & Access