Pass Array to Functions in C

Write a C Program to pass an array to functions. Or How to pass an array to functions in C programming with a practical example.

Pass Array to Functions in C Example 1

In this Pass Array to Functions 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 array element to the function that we created earlier.

/* Pass Array to Functions in C  */

#include<stdio.h>
 
void PrintArray(int a)
{
    printf("Array Element = %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.

Array Element = 1 
Array Element = 2 
Array Element = 3 
Array Element = 4 
Array Element = 5 
Array Element = 6 

Pass Array to Functions Example 2

Instead of passing individual array element, you can directly pass array to functions. Please refer C Program to find Sum of Array Elements article to understand the logic behind this program.

/* C Program to Pass Array to Functions  */

#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 in C 2

Pass Array to Functions 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 Array Elements to understand the logic behind this C program.

/* C Program to Pass Array to Functions */

#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 

Program to Pass Array to Functions Example 4

This program shows you how to pass multi-Dimensional arrays to functions. I suggest you refer to the C Program to find the sum of each row in a Matrix article to know the logic behind this program.

/* Pass Array to Functions in C  */

#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