Passing array to function in C is a very important topic because arrays are rarely useful unless they can be processed inside functions. In real programs, arrays are often filled, printed, searched, sorted, or modified by helper functions. That is why understanding how arrays behave when passed to functions is essential for writing clean and reusable C code.
Many beginners expect an array to be passed in the same way as a normal variable, but arrays behave differently in function calls. When an array is passed to a function, what the function effectively receives is the address of the first element. This is why array parameters are closely related to pointers in C. In this article, we will understand passing array to function in C, syntax, examples, how it works internally, array modification behavior, and common mistakes.
What Does Passing Array to Function in C Mean?
Passing array to function in C means giving a function access to the elements of an array so that the function can read or modify them. Instead of copying the entire array into the function, C typically passes the base address of the array.
That is why array passing in C is efficient. The function does not need to receive a full duplicate of the array data just to work with it.
When an array is passed to a function in C, the function works with the address of the first element, not a full copied array value.
Syntax of Passing Array to Function in C
A function parameter for an array can be written in more than one valid way.
void display(int arr[], int size);
void display(int arr[5], int size);
void display(int *arr, int size);Inside the function parameter list, these forms are treated similarly for one-dimensional arrays. The size written inside the brackets is not used the same way as in normal array declarations.
| Parameter form | Meaning |
|---|---|
int arr[] | Array-style function parameter |
int arr[5] | Looks sized, but behaves like array parameter |
int *arr | Pointer-style equivalent form |
Example of Passing Array to Function in C
The following program passes an integer array to a function and prints all elements.
#include <stdio.h>
void display(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
}
int main(void)
{
int numbers[5] = {10, 20, 30, 40, 50};
display(numbers, 5);
return 0;
}Here, the array numbers is passed to display() along with the size. The function prints the values one by one.
Why Size is Usually Passed with the Array
When an array is passed to a function, the function does not automatically know how many elements the array contains. That is why programmers usually pass the array size as a separate argument.
Without the size, the function cannot safely decide how far it should loop.
| What is passed | Why it matters |
|---|---|
| Array base address | Lets the function access elements |
| Array size | Lets the function know loop boundary |
Can a Function Modify the Original Array?
Yes. Since the function works with the array’s memory through the passed address, it can modify the original elements.
#include <stdio.h>
void update(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
{
arr[i] = arr[i] + 1;
}
}If this function is called with an array, the actual array values in the caller will change.
How Array Passing Works Internally
When an array name is used in a function call, it usually decays into a pointer to its first element. So if numbers is passed to a function, the function effectively receives the address of numbers[0].
This is why array parameters and pointer parameters are so closely connected in C.
| Expression | Meaning |
|---|---|
numbers | Base address of first element in most expressions |
numbers[0] | First stored value |
&numbers[0] | Address of first element |
Passing Character Array to Function in C
Character arrays are also passed to functions in the same general way. This is common in string-related functions.
#include <stdio.h>
void showString(char str[])
{
printf("%s", str);
}This works because the character array is passed as the address of its first character.
Passing 2D Array to Function in C
Passing a one-dimensional array is simple, but 2D arrays need more care. The function usually needs to know the column size so it can calculate element positions correctly.
void printMatrix(int arr[][3], int rows)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}This requirement comes from how multidimensional arrays are laid out in memory.
Array Parameter vs Normal Variable Parameter
| Point | Normal variable | Array parameter |
|---|---|---|
| Passing style | Value is copied | Base address is effectively used |
| Modification inside function | Usually affects local copy only | Can affect original array elements |
| Need separate size? | No | Usually yes |
This is why arrays feel different from ordinary variables when they are passed to functions.
Common Mistakes When Passing Array to Function in C
- forgetting to pass the size of the array
- assuming the function knows the number of elements automatically
- thinking the array is fully copied into the function
- using wrong loop limits inside the function
- confusing array parameter syntax with true array copying
| Mistake | Problem | Better practice |
|---|---|---|
| No size argument | Unsafe traversal | Pass size separately |
| Wrong loop boundary | Out-of-bounds access | Use correct size in loops |
| Assuming copy behavior | Unexpected modification of original array | Remember array data can be modified |
| Wrong 2D parameter declaration | Compilation or logic errors | Specify required later dimensions |
Best Practices for Passing Array to Function in C
- Always pass the array size for one-dimensional arrays when needed.
- Use clear parameter names such as
arr,size, androws. - Remember that array data can be modified inside the function.
- Use correct loop boundaries in every function that handles arrays.
- For multidimensional arrays, define required later dimensions correctly.
- Think carefully about whether the function should only read or also update the array.
FAQs
How do you pass an array to a function in C?
You pass the array name in the function call and usually pass the size separately, such as display(arr, size).
Does C copy the whole array when passing it to a function?
No. The function effectively works with the address of the first element, not a full copied array value.
Why do we pass the size of the array to a function?
Because the function usually cannot automatically know how many elements the array contains.
Can a function modify the original array in C?
Yes. Since the function works with the array’s address, it can modify the original elements.
Is int arr[] the same as int *arr in function parameters?
For one-dimensional array parameters, they are treated similarly in function declarations.
Can 2D arrays also be passed to functions in C?
Yes, but the function usually needs to know the column size so it can access elements correctly.