In this tutorial, we will learn about passing arrays to functions in C programming.
Like other values of variables, arrays can be passed to a function. Let us see how to pass an entire array to a function. Both one-dimensional arrays and multidimensional arrays can be passed as function arguments.
While passing one-dimensional array to the function name of the array is passed as actual arguments and array variable with subscript is passed as formal arguments.
void func_name( int *name )
{
.......
}
void func_name( int name[10] )
{
.......
}
void func_name( int name[] )
{
.......
}
Example: C program to pass an array to the function that contains marks obtained by the student. Then display the total marks obtained by the student
#include <stdio.h>
int total_marks(int a[]);
int main()
{
int total = 0, marks[]={40,80,75,90,88};
printf("Total marks = %d",total_marks(marks));
return 0;
}
int total_marks(int a[])
{
int sum=0,i;
for (i=0;i<5;i++)
sum=sum+a[i];
return sum;
}
Output
Total marks = 373
Explanation:
Here, the total_marks( )
function is used to calculate total marks. The for
loop is used to access the array elements.
[adsense1]
Like simple arrays, multidimensional arrays can be passed to the function. Two-dimensional array is indicated by two sets of brackets with array variable. The size of second dimension must be specified.
Example: C program to display the 3*3 matrix of given elements.
#include <stdio.h>
void matrix( int m[][3] )
{
int i,j;
printf("Matrix is :\n");
for ( i=0;i<3;i++ )
{
for ( j=0;j<3;j++)
{
printf("%d",m[i][j]);
}
printf("\n");
}
}
int main()
{
int m[3][3] = {1,2,3,4,5,6,7,8,9};
matrix(m);
return 0;
}
Output
Matrix is :
1 2 3
4 5 6
7 8 9
Note: It is a good idea to pass arrays by reference rather than value for performance reason. In the case of large arrays, passing by value will consume higher storage.
#include <stdio.h>
#define SIZE 5
void changeArray( int x[], int s);
void changeElement( int y );
int main ()
{
int arr[SIZE] = {0, 1, 2, 3, 4};
int i; //counter
printf("passing entire array by reference.\n");
printf("values of original array:\n");
for(i = 0; i < SIZE; i++)
{
printf("%3d", arr[i]);
}
changeArray(arr, SIZE); //passing array by reference
printf("\nvalues of changed array:\n");
for(i = 0; i < SIZE; i++)
{
printf("%3d", arr[i]);
}
printf("\nlet's see the value by passing array element\n"
"\nvalue of arr[3] = %d", arr[3]);
changeElement(arr[3]); //passing array element arr[3] by value
printf("\nvalue of arr[3] = %d", arr[3]);
}
void changeArray(int x[], int s)
{
int j; //counter variable
for (j = 0; j<= SIZE; j++)
{
x[j] = x[j] * 2;
}
}
void changeElement( int y)
{
y = y * 2; //multiplying array element by 2
printf("\nvalue of 3rd element in changeElement = %d ", y);
}
Output
passing entire array by reference. values of original array: 0 1 2 3 4 values of changed array: 0 2 4 6 8 let's see the value by passing array element value of arr[3] = 6 value of 3rd element in changeElement = 12 value of arr[3] = 6
Explanation
In the above program, we can see the difference between passing an array by reference and element by value.
The program first prints the value of array and it is passed to changeArray()
function, where elements are multiplied by 2. The modified result is printed.
In the second part, the program first prints the third element of modified array. After this third element of an array is passed to changeElement()
function by value, where it is multiplied by 2 and value is printed.
Finally, in the main
function the value of third element of an array is printed again but the value remains same.
Note: We can use const
(constant) type qualifier to prevent modification of array elements in a function.