Arrays to Functions
Passing arrays to functions in C
In C programming, an array can be passed to a function so that the function can process its elements. For example, a function can calculate the sum, average, or largest element of an array.
Definition
Passing an array to a function means providing the array's starting address to a function so that it can access and process the array elements.
In C, when an array is passed as a function argument, it is adjusted to a pointer to its first element. The array elements are not copied into a new array.
Syntax
return_type function_name(data_type array[], int size);
Example:
void display(int a[], int size);
Here:
- void means the function does not return a value.
- int a[] represents the array parameter.
- size specifies the number of elements the function should process.
Example 1: C Program passing an array to display its elements
#include
void display(int a[], int size)
{
int i;
for (i = 0; i < size; i++)
{
printf("%d ", a[i]);
}
}
int main(void)
{
int numbers[5] = {10, 20, 30, 40, 50};
display(numbers, 5);
return 0;
}
Output:
10 20 30 40 50
How does it work?
- Calling function:
display(numbers, 5);The array name provides access to its first element, and 5 provides the number of elements. - Receiving parameters:
void display(int a[], int size)The parameter a can access the original array elements, and size is 5. - Processing elements: The loop accesses a[0], a[1], a[2], a[3], and a[4] and displays each value.
Example 2: C Program passing an array to calculate the sum
#include
int calculateSum(int a[], int size)
{
int i, sum = 0;
for (i = 0; i < size; i++)
{
sum = sum + a[i];
}
return sum;
}
int main(void)
{
int a[5] = {10, 20, 30, 40, 50};
int result;
result = calculateSum(a, 5);
printf("Sum = %d\n", result);
return 0;
}
Output:
Sum = 150
The function receives the array and its size, adds all the elements, and returns the sum to main().
Modifying array elements inside a function
A function can also modify the original array elements because its array parameter provides access to those elements.
#include
void doubleElements(int a[], int size)
{
int i;
for (i = 0; i < size; i++)
{
a[i] = a[i] * 2;
}
}
int main(void)
{
int a[4] = {1, 2, 3, 4};
int i;
doubleElements(a, 4);
for (i = 0; i < 4; i++)
{
printf("%d ", a[i]);
}
return 0;
}
Output:
2 4 6 8
The original array is modified. This happens because the function accesses the same array elements through its parameter.
Passing a two-dimensional array to a function
For a two-dimensional array, the number of columns must be specified in the function parameter type.
#include
void display(int a[][3], int rows)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
int main(void)
{
int a[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
display(a, 2);
return 0;
}
Output:
1 2 3
4 5 6
Notice that int a[][3] leaves the row count unspecified but specifies the column count as 3.
Practice questions
- Explain how arrays are passed to functions in C with a suitable example.
- Write a C program to calculate the sum of array elements using a function.
- Explain how a function can modify array elements.
- Explain how a two-dimensional array is passed to a function.