Passing an array to a function in C

In this article, we will learn how to pass an individual array element or whole array to a function in C. First, we have created a program that passes an entire array to a function. And then I created a program that passes individual array elements to a function.

Passing the entire array to function

Let's learn, with the program given below, how to pass a whole array to any function in C. The passed array can be used in functions just like it can be used in the main function.

#include<stdio.h>
#include<conio.h>
void func(int arr[]);
int main()
{
    int arr[10], i;
    printf("Enter 10 array elements: ");
    for(i=0; i<10; i++)
        scanf("%d", &arr[i]);
    printf("\nPassing array to the function...\n");
    func(arr);
    getch();
    return 0;
}
void func(int arr[])
{
    int i;
    printf("\nThe array is:\n");
    for(i=0; i<10; i++)
        printf("%d ", arr[i]);
}

As you can see from the above program, the whole array (everything inside the array) gets passed into a function named func(), which performs the operation.

The above program was written inside the Code::Blocks IDE; therefore, after a successful build and run, here is the sample output:

c program pass whole array to function

Now provide any 10 array elements (numbers) and then press the ENTER key. After this, you will get a message saying "Passing array to the function," and then the complete array gets passed to the function named func(). Inside that function, all the 10 entered array elements get printed back on the output screen; here is the second screenshot of the sample run:

pass whole array to function in c

Program Explained

Passing the individual array elements to a function in C

Now let's create another program that passes individual array elements to a function one by one.

#include<stdio.h>
#include<conio.h>
void check(int num);
int main()
{
    int arr[10], i;
    printf("Enter 10 Array elements:\n");
    for(i=0; i<10; i++)
        scanf("%d", &arr[i]);
    printf("\nChecking each elements for Even/Odd..\n");
    for(i=0; i<10; i++)
        check(arr[i]);
    getch();
    return 0;
}
void check(int num)
{
    if(num%2==0)
        printf("%d is Even\n", num);
    else
        printf("%d is Odd\n",num);
}

As you can see here, all 10 elements of the array get passed to the function check(), which checks whether the element is an even number or an odd number. Here is its sample run:

c program pass array element to function

Now enter all 10 numbers (array elements) and then press ENTER. Here is the sample output:

pass array element to function c

Program Explained

Here is the modified version of the above program. The user is allowed to define the size of the array before entering elements for it:

#include<stdio.h>
#include<conio.h>
void checkEven(int e);
void checkOdd(int o);
static int n1=0, n2=0;
int main()
{
    int arr[100], i, size;
    printf("How many elements you want to store: ");
    scanf("%d", &size);
    printf("Enter all %d Array elements:\n", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    printf("\nChecking each elements for Even/Odd..\n");
    printf("\nList of all Even numbers: ");
    for(i=0; i<size; i++)
        checkEven(arr[i]);
    printf("\nList of all Odd numbers: ");
    for(i=0; i<size; i++)
        checkOdd(arr[i]);
    getch();
    return 0;
}
void checkEven(int e)
{
    if(e%2==0)
    {
        if(n1==0)
            printf("%d", e);
        else
            printf(", %d", e);
        n1++;
    }
}
void checkOdd(int o)
{
    if(o%2 != 0)
    {
        if(n2==0)
            printf("%d", o);
        else
            printf(", %d", o);
        n2++;
    }
}

This program is designed to ask the user at run-time how many elements he/she wants to store, say 10, and then prompt the user to enter all elements (say 10). Finally, the program will list out all the even and odd numbers. Let's take a look at its output with the sample run given below:

c program pass array to function

Now enter 10 as the size of the array, then provide all the 10 elements, and then press ENTER to see all the even and odd numbers from those entered 10 numbers:

pass array to function c program

Note: A variable of the static type holds or remembers its previous value throughout the program.

C Quiz


« Previous Program Next Program »


Liked this post? Share it!