Passing 1-D Array to a Function in C
Last updated on July 27, 2020
In the chapter One Dimensional Array and Function in C , we have discussed that when an array is passed to a function, the changes made by the function affect the original array. After studying about pointers we are in the position to understand why this happens. But before we study this, I want to make a few points clear.
In the above-mentioned chapter, we have also learned that when a 1-D array is passed to the function, it is optional to specify the size of the array in the formal arguments. So if we are passing an array of 5 integers then the formal argument of a function can be written in the following two ways.
int my_arr[5] = [11,44,66,90,101];
1st way:
1 2 3 4 | void function(int a[]) // here the size of the array is omitted
{
// statements;
}
|
2nd way:
1 2 3 4 | void function(int a[5]) // here the size of the array is specified
{
// statements;
}
|
In the chapter Pointers and 1-D arrays, we have also learned that name of the array is a constant pointer to the 0th element of the array. In our case my_arr
is a pointer to 0th element of the array, in other words, my_arr
points to the address of element 11. So the base type of my_arr
is a pointer to int
or (int *)
. Hence the formal argument of a function can be also be declared as pointer to int
or (int *)
:
3rd way:
1 2 3 4 | void function(int *a)
{
// statements;
}
|
Essentially in all the three cases base type of a
is a pointer to int
or (int *)
, we are just using three different ways to represent them.
Okay let's get back to our original discussion: Why do the changes made to an array inside a function affect the original array? The following program answers this question.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include<stdio.h>
void new_array(int a[]);
int main()
{
int my_arr[] = {1,4,9,16,23}, i;
printf("Original array: \n\n");
for(i = 0; i < 5; i++)
{
printf("%d ", my_arr[i]);
}
my_func(my_arr);
printf("\n\nModified array : \n\n");
for(i = 0; i < 5; i++)
{
printf("%d ", my_arr[i]);
}
// signal to operating system program ran fine
return 0;
}
void my_func(int a[5])
{
int i;
// increment original elements by 5
for(i = 0; i < 5; i++)
{
a[i] = a[i] + 5;
}
}
|
Expected Output:
1 2 3 4 5 6 7 | Original array:
1 4 9 16 23
Modified array:
6 9 14 21 28
|
How it works:
We know that my_arr
is a pointer to the first element of the array. So we can pass my_arr
to the function my_func()
without using &
operator. In line 15, the my_func()
is called with an actual argument of my_arr
which is then assigned to a
. Again note that we are passing the address of my_arr
to a
, that means we are using call by reference instead of call by value. So now both my_arr
and a
points to the same array. Inside the function, we are using for loop to increment every element of the array by 5
. Since we are operating on the original array all the changes made here effect the original array.
Load Comments