Passing Array to Function in C

by | Sep 29, 2021 | C

Home » C » Passing Array to Function in C

Introduction

Passing an element of the array and passing an array in a function, both are completely valid in C language.

Look at the code written below where we pass a single element of the array in the function as an argument.

Program

#include<stdio.h>
int show(int x)
{
            printf("%d\n",x);
}
int main()
{
            int A[8]={8,7,6,5,4,3,2,1};
            show(A[5]);
            show(A[6]);
            return 0;
}

 

Output

Passing Array to Function in C

Explanation

The ‘show’ function is taking an ‘int’ type argument and in the ‘main’ function we pass a single element of the array in the function call and we got the elements displayed on the output window. So, it is very simple to use arrays in the function.

Now, let us move ahead and see how things will work when we pass an entire array in the function.

Pass an entire array in the function

To pass the entire array in the function as an argument just pass the array name and it will work fine. Since we know the name of the array is the pointer to the first element of the array which means it is having the address value of the first element. The trick is that if we pass the name of the array as an argument then the address of the array will get passed as the function argument.

However, it might be difficult for you to understand the theory written above but the code written below will clear everything.

Program

#include<stdio.h>
float average(float x[])
{
            int i;
            float avg;
            float sum=0;
            for(i=0;i<5;i++)
            {
                        sum=sum+x[i];
            }
            avg=sum/5;
            return avg;
}
int main()
{
            float A[5]={10.1,20.2,30.3,40.4,50.5};
            float B;
            B=average(A);
            printf("Average:%.2f\n",B);
            return 0;
}

 

Output

Pass an entire array in the function

Explanation

float average(float x[])

The function named ‘average’ of ‘float’ type is taking a ‘float’ type array as an argument. In the function definition ‘for’ loop is updating the value of ‘sum’ by adding the array elements.

In the ‘main’ function an array is declared and initialized with values.

B=average(A);

We just passed ‘A’ as an argument in the ‘average’ function which means we are passing a pointer. As discussed earlier, ‘A’ is a pointer to the first element of the array. So, we have passed the pointer.

In the function definition, the ‘for’ loop is used to update the value of ‘sum’ by adding values of the array one by one.

avg=sum/5;

To find the average, ‘sum’ is divided by 5(division can also be done with 5.0) which will assign a ‘float’ type value in the ‘avg’ variable. Thus, the value of average is obtained through this function.

The value of average obtained from the ‘average’ function is printed as the output according to our choice.

So, the task was looking quite complicated i.e., using arrays in functions but with basic concepts like pointers, everything works so smooth.

In the above example, the size of the array was already known. But what if the user wants to use the array according to his/her choice and wants to enter the size of the array too. In such cases, the size of the array will be passed as the second argument to the function.

Pass an entire array with size in the function

Study the code written below in detail to get deep knowledge of this concept.

Program

#include<stdio.h>
float average(float x[], int size)
{
            int i;
            float avg;
            float sum=0;
            for(i=0;i<size;i++)
            {
                        sum=sum+x[i];
            }
            avg=sum/size;
            return avg;
}
int main()
{
            int size,j;
            printf("Enter the size of the array\n");
            scanf("%d",&size);
            float A[size];
            float B;
            for(j=0;j<size;j++)
            {
                        printf("Value of A[%d]:",j);
                        scanf("%f",&A[j]);
            }
            B=average(A,size);
            printf("Average:%.2f\n",B);
            return 0;
}

 

Output

Pass an entire array with size in the function

Explanation

The code is the almost same as the previous one. In the ‘average’ function we passed ‘float’ type array and ‘int’ type size.

In the ‘main’ function we are giving the array size. Both iterators ‘i’ and ‘j’ iterate up to the value of ‘size’ in the ‘average’ and ‘main’ functions respectively.

B=average(A, size);

In the function call, we will pass the array name as well as the size of the array that we had entered.

So, it is up to you, whatever size and the data we enter, the code will work accordingly and we will get our output.

 

Passing of an array to the function using pointers

Program

#include<stdio.h>
int point(int *p)
{
            int i;
            for(i=0;i<5;i++)
            {
                        printf("n[%d]=%d\n",i,*p);
                        p++;
            }
}
int main()
{
            int n[]={5,10,15,20,25};
            point(n);
           
            return 0;
}

 

Output

Passing of an array to the function using pointers

Explanation

point(n);

Passing the array name as the argument in the function call means we are passing the pointer since ‘n’ is a pointer to the first element of the array.

Why do we pass the pointer to the function?

int point (int *p) this function declaration is taking the pointer as an argument to the function so we have to pass a pointer as an argument in the function call.

In the ‘for’ loop, for ‘i=0’ value of n[0] is printed since *p represents the value of n[0]. After that p++ increases *p to *(p+1) in the second iteration for ‘i=1’ value of n[1] gets printed.

This loop continues till ‘i’ reaches 4 and the value of n[4] also gets printed.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author