Pass Array to Functions in C

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 

Example 2

Instead of passing individual array element, you can directly pass array to functions. Please refer C Program to find 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;
}
Pass Array to Functions 2

Example 3

Instead of passing an array element, you can pass the reference of it. I mean, array call by reference directly.

Please refer C 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 

Example 4

This program shows you how to pass multi-Dimensionals to functions. 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