Write a C Program to pass an array to functions. Or How to pass an array to functions with a practical example.
Pass Array to Functions in C Example
In this program, we created a function that accepts an integer variable and prints that variable as the output. Within the main program, we used for loop to iterate the array and pass each element to the function that we created earlier.
#include<stdio.h>
void PrintArray(int a)
{
printf("Item = %d \n", a);
}
int main()
{
int array[] = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < 6; i++)
{
PrintArray(array[i]);
}
return 0;
}
Output. Please refer to Functions Call By Value and Call by Reference and for loop article.
Item = 1
Item = 2
Item = 3
Item = 4
Item = 5
Item = 6
How do you pass an entire array to a function in C?
Instead of passing individual array elements, you can directly pass the array to functions. Please refer to the C Program to find the Sum of Elements article to understand the logic behind this program.
#include<stdio.h>
int SumofNumbers(int a[], int Size)
{
int Addition = 0;
int i;
for(i = 0; i < Size; i++)
{
Addition = Addition + a[i];
}
return Addition;
}
int main()
{
int i, Size, a[10];
int Addition;
printf("Please Enter the Size of an Array: ");
scanf("%d", &Size);
printf("\nPlease Enter Array Elements\n");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}
Addition = SumofNumbers(a, Size);
printf("Sum of All Elements in an Array = %d \n", Addition);
return 0;
}

How to Pass Array Reference to Functions in C using pointer?
Instead of passing an array element, you can pass the reference of it. I mean, array call by reference directly. Please refer to the Program to print Elements to understand the logic behind this C program.
#include<stdio.h>
void PrintArray(int *a, int Size)
{
int i;
printf("\n **** Elemenst in this Array are : ****\n");
for (i = 0; i < Size; i++)
{
printf(" Element at Array[%d] = %d \n", i, a[i]);
}
}
int main()
{
int Array[50], i, Number;
printf("\nPlease Enter Number of elements in an array : ");
scanf("%d", &Number);
printf("Please Enter %d elements of an Array : ", Number);
for (i = 0; i < Number; i++)
{
scanf("%d", &Array[i]);
}
PrintArray(Array, Number);
return 0;
}
Please Enter Number of elements in an array : 5
Please Enter 5 elements of an Array : 10 12 14 23 56
**** Elemenst in this Array are : ****
Element at Array[0] = 10
Element at Array[1] = 12
Element at Array[2] = 14
Element at Array[3] = 23
Element at Array[4] = 56
How to pass a 2D array to a function in C?
When passing a 2D or two-dimensional array, we must specify the column dimensions (i.e., the number of columns); the first dimension (i.e., the number of rows) is optional.
In the following example, we pass a 2D array to the user-defined function as the first argument. Next, we used the rows and columns as the other two arguments. It is the most efficient approach, and we can utilize this function for any dynamic arrays where the rows and columns are changed as per the requirement.
I suggest you refer to the find the sum of each row in a Matrix article to know the logic behind this program.
#include<stdio.h>
void AddRows(int arr[10][10], int i, int j)
{
int rows, columns;
for(rows = 0; rows < i; rows++)
{
int Sum = 0;
for(columns = 0; columns < j; columns++)
{
Sum = Sum + arr[rows][columns];
}
printf("The Sum of Elements of a Rows in a Matrix = %d \n", Sum );
}
}
int main()
{
int i, j, rows, columns, a[10][10];
printf("Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[rows][columns]);
}
}
AddRows(a, i, j);
return 0;
}
Please Enter Number of rows and columns : 3 3
Please Enter the Matrix Elements
10 20 30
40 50 60
70 80 90
The Sum of Elements of a Rows in a Matrix = 60
The Sum of Elements of a Rows in a Matrix = 150
The Sum of Elements of a Rows in a Matrix = 240
How to pass a multidimensional array to a function in C?
Similar to the 2D array, we can use the same approach to pass a multidimensional array to a function. However, the function requires all dimensions except the first dimension. So, we must specify the total number of rows and columns while defining the function.
In the following program, we used the first argument to mention the tables, rows, and columns required for the 3D array. Next, we passed a multidimensional array as the fourth argument to the function.
#include <stdio.h>
void display3DArrayItems(int a, int b, int c, int arr[a][b][c])
{
for (int i = 0; i < a; i++)
{
for (int j = 0; j < b; j++)
{
for (int k = 0; k < c; k++)
{
printf("%d ", arr[i][j][k]);
}
printf("\n");
}
printf("\n");
}
}
int main()
{
int arr[2][2][3] = {
{{10, 20, 30},
{40, 50, 60}},
{{70, 80, 90},
{55, 77, 88}}};
display3DArrayItems(2, 2, 3, arr);
return 0;
}
10 20 30
40 50 60
70 80 90
55 77 88
How to pass a dynamically allocated array to a function in C?
Similar to any other pointer, we can pass a dynamically allocated array to a function. In the following program, the user-defined function accepts a pointer pointing to the first item of an array as the first argument. Next, we passed the size of an array as the second argument to print the array items.
Within the main program, we use the malloc function to allocate memory for the 5 array items. As it is an integer array, it is allocated 20 bytes (5 items * 4 bytes). The for loop dynamically assigns array elements.
#include <stdio.h>
#include <stdlib.h>
void printArrayItems(int *a, int size)
{
for (int i = 0; i < size; i++)
printf("%d ", a[i]);
}
int main()
{
int size = 5;
int *num = malloc(size * sizeof(int));
for (int i = 0; i < size; i++)
{
num[i] = (i + 1) * 10;
}
printArrayItems(num, size);
free(num);
return 0;
}
10 20 30 40 50