passing array to function in c
C Programming

Passing Array to Function in C Language

Passing Array to Function in C Language

  • In this tutorial we will learn how to pass array to function as an argument in C.
  • In C programming there are some situations in which we have to pass the more than one than one variables of same types as an argument to function.
  • For example we have to sort 8 elements of integer type. Here In this case instead of declare the 10 variables separately and pass them to function we can declare and initialize an array of 8 elements and pass that array to function.
  • So whenever we need to pass a list of elements then we can do this by passing array as an argument to function.

Passing Array to Function Example

Program for Passing array to function in C Language is explained with example here.

Consider a program to find the minimum element in array using function.

Find minimum element in array using function in C

#include<stdio.h>

#include<conio.h>

int minarray( int arr[] , int size)

{

int min=arr[0];

int i=0;

for(i=1;i<size;i++)

{

             if(min>arr[i])

           {

                    min=arr[i];

            }

}

return min;

}

void main()

{

int i=0,min=0;

int numbers[]={4,5,7,3,8,9}; //declaration and intialization of array

min=minarray(numbers,6);//passing array with size at the time of function calling

printf(“minimum number is %d \n”,min);

getch( );

}

  • Here in this program we defined a function named as minarray( ).
  • In this function array named as arr[ ] and size of this array is passed as parameters. This function will return the minimum element in the array.
  • Array is declared and initialized in main() function.
  • minarray( ) function is called in main() function.

When we execute the above program following output is displayed.

Output

pass array to function

In this program we declare and defined a function minarray() to find the minimum element in array. We pass an array numbers as an argument to this minarray() function.

When we compile and run this program then we will get the minimum element of array as an output.

Advantages of Passing Arrays to Functions in C Language

There are following advantages of passing arrays to function in C Language.

  • Passing similar elements as an array takes less time than passing each element to a function as we are only passing the base address of the array to the function, and other elements can be accessed easily as an array is a contiguous memory block of the same data types.
  • As we pass reference of base address of the array, this means the compiler doesn’t create a copy of the array to process inside function which is faster and less memory-intensive compared to passing arguments by value.
  • Because arrays are passed by reference to functions, this prevents stack memory overflow in the case of recursive functions.

Disadvantages of Passing Arrays to Functions

  • We can get garbage values if the user tries to access values beyond the size of the array, which can result in wrong outputs.
  • To prevent this, the bound check should be used before accessing the elements of an array, and also, array size should be passed as an argument in the function.
  • If the memory space is more than elements in the array, this leads to a wastage of memory space.
  • Special care is required when dealing with a multidimensional array as all the dimensions are required to be passed in function.

Conclusion and Summary

  • In this tutorial we have discussed the concept of passing array to function in c with example and c program.
  • Advantages and disadvantages of passing array to function in C is also explained.

Leave a Reply

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