Passing Array to Function in C (With Examples & Output)
Learn how to pass arrays to functions by reference and by value with complete code examples
📚 Part of the Functions in C series. New to functions? Start with: Introduction to Functions in C
Last Updated: September 21, 2026
About the Author
Sankalan Data Technologies
Programming & Data Analytics Tutorials
Published by Sankalan Data Technologies. Our tutorials and data guides are independently developed and curated to provide practical, hands-on learning resources for Python, SQL, and data analytics practitioners.
Quick Answer: Passing Array to Function in C
To pass an array to a function in C, pass the array name (which acts as a pointer to the first element) and the array size as separate parameters.
void total(int a[], int size) {
/* function body */
}
int main() {
int salary[] = {4000, 4500, 2300, 4623, 1540};
total(salary, 5); /* pass array name and size */
return 0;
}
Changes made inside the function affect the original array because arrays are passed by reference.
What You'll Learn in This Tutorial
- Why Pass Arrays to Functions?
- Passing Array by Reference
- Passing Array by Value
- By Reference vs By Value
- Practice Questions
Why Pass Arrays to Functions?
An array is a collection of similar data types stored in contiguous memory blocks. When working with multiple values, it's often more convenient to pass the entire array to a function instead of individual elements.
Consider this: if you need to add a list of numbers, passing them as an array lets the function work with any number of elements. Passing each value individually would require a different function for every array size.
In Simple Words
Pass the array name and size to the function. The array name acts as a pointer to the first element, so the function can access all array elements.
From experience: The most common mistake students make is forgetting to pass the array size along with the array. Unlike some higher-level languages, C has no built-in way to know how long an array is inside a function. If you forget the size, the function will read past the end of the array and cause undefined behavior. Always pass both the array and its size.
Passing array to function in C: the base address is passed
1. Passing Array by Reference (Recommended)
When you pass the array name to a function, C treats it as a pointer to the first element. The function receives the base address of the array, so any changes made inside the function affect the original array.
How It Works
Only the base address of the array is passed to the function — not the entire array. This is efficient because passing the address takes the same memory regardless of array size.
C Program: Passing Array by Reference
#include <stdio.h>
void total(int a[], int size) {
int i = 0, tot = 0;
printf("Array Elements:\n");
for (i = 0; i < size; i++) {
printf("%d\n", a[i]);
tot = tot + a[i];
}
printf("Sum of array elements = %d\n", tot);
}
int main() {
int n = 5;
int salary[] = {4000, 4500, 2300, 4623, 1540};
total(salary, n);
return 0;
}
Output
Array Elements:
4000
4500
2300
4623
1540
Sum of array elements = 16693
How This Program Works (Step-by-Step)
- Step 1: Declare integer variable
n = 5as the array size. - Step 2: Declare and initialize the
salaryarray with 5 values. - Step 3: Call
total(salary, n)— passes the array base address and size. - Step 4: Inside
total(), the loop accesses each element usinga[i]. - Step 5: The function displays each element and accumulates the sum.
- Step 6: The final sum
16693is displayed.
2. Passing Array by Value
When you pass individual array elements to a function, each element is copied into a local parameter. Any changes made inside the function do not affect the original array.
This is useful when the function needs to work with a value without modifying the original data.
C Program: Passing Array Elements by Value
#include <stdio.h>
void show(int p) {
p += 5;
printf("Inside function, p = %d\n", p);
}
int main() {
int a[5] = {12, 34, 54, 21, 43};
int i;
for (i = 0; i < 5; i++) {
show(a[i]);
printf("In main, a[%d] = %d\n", i, a[i]);
}
return 0;
}
Output
Inside function, p = 17
In main, a[0] = 12
Inside function, p = 39
In main, a[1] = 34
Inside function, p = 59
In main, a[2] = 54
Inside function, p = 26
In main, a[3] = 21
Inside function, p = 48
In main, a[4] = 43
Note: Even though the function adds 5 to p, the original
array a remains unchanged because each element was passed by value (a copy).
By Reference vs By Value: Key Differences
| Feature | By Reference | By Value |
|---|---|---|
| What is passed | Base address of array | A copy of each element |
| Changes affect original? | Yes | No |
| Efficiency | High (only address passed) | Low (each element copied) |
| Syntax | function(arr, size) |
function(arr[i]) |
| Best for | Processing entire arrays | Working with individual values |
Common Mistakes to Avoid
- Forgetting to pass the array size — C functions have no built-in way to know the array length
- Using
sizeof()inside the function — It returns the pointer size, not the array size - Returning a local array — Never return a local array from a function; it goes out of scope
- Modifying a "by value" parameter expecting changes — Changes are lost when the function returns
- Off-by-one in loops — Use
i < size, noti <= size
Practice Questions
Try these exercises to reinforce your understanding:
- Write a program that passes an array to a function and returns the largest element.
- Write a program that passes an array to a function and reverses it in place.
- Write a program that counts how many elements in an array are even or odd.
- Write a program that passes a 2D array to a function and prints it.
- Write a program that passes an array to a function and sorts it using bubble sort.
Hint: Remember to pass both the array and its size to any function that needs to iterate over all elements.
Why This Matters
- Foundation Concept: Passing arrays is essential for writing modular C code
- Exam Relevance: This is a common topic in BCA, MCA, and engineering exams
- Real-World Use: Every program that processes lists of data uses this pattern
- Interview Preparation: Array passing is a frequent interview question
Frequently Asked Questions
How do you pass an array to a function in C?
In C, you pass an array to a function by passing its name. The array name acts as a pointer to the first element. You can also pass individual elements by value. The function receives either a pointer to the array or copies of individual values.
What is the difference between passing an array by value and by reference in C?
Passing by reference means passing the array name, which passes the base address. Changes made in the function affect the original array. Passing by value means passing individual elements, which copies values. Changes do not affect the original array.
Do I need to pass the array size to the function in C?
Yes, when passing an array by reference, you must also pass the array size as a separate parameter. The function cannot determine the array size from the array name alone in C.
Can a function return an array in C?
A C function cannot return an entire array directly. However, you can return a pointer to an array, or pass an array to the function and modify it in place.
What is the correct syntax for passing an array to a function in C?
The correct syntax is: void functionName(int arr[], int size). You can also write it as void functionName(int *arr, int size). Both forms are equivalent in C.
Why does modifying an array inside a function affect the original array in C?
Because arrays in C are passed by reference. The function receives the base address of the array, so any changes made through that address affect the original array.
Further Reading
Related Tutorials
Previous Topic: Recursion Functions in C | Next Topic: Scope, Visibility and Lifetime of Variables