Home >>C Tutorial >Arrays to function in C

Pass arrays to a function in C

Pass arrays to a function in C

Passing more than one variable of the same type to a function is required by various general problems in the C language. It is seen as an important task to Pass arrays to a function in C as to be passed as the actual parameters from the main function some amount of numbers is required. For instance, a function in C that sorts the 10 elements in the ascending order. In order to be passed as the actual parameters from the main function, these type of function will require 10 numbers. In this case, instead of declaring those 10 numbers that are different from each other and then passing into the function, Users can declare and initialize an array and pass that into the function, instead of declaring 10 different numbers and then passing into the function. Doing this will automatically resolve all the complexity as the function will now work for any number of provided values.

It is a known fact that the array_name consists of the address of the first element. The users must take note that they only need to pass the name of the array in the function that is mainly intended to accept an array. The array that is specified by the array name and defined as the actual parameter, and will be automatically referred by the array that is defined as the formal parameter in C language.

Here is the syntax to pass an array to the function:

functionName(arrayName);//passing array  

Let's have a look at the methods that are used to declare a function that receives an array as an argument

Here are the three ways to declare the function that is intended to receive an array as an argument:

1. The First Way

The first way includes the most widely used technique that is declaring blank subscript notation [].
Here is the syntax for the same:

return_type function(type arrayname[]) 

2. The Second Way

The second way is also a most widely used technique used optionally to the first way. It involves defining the size in subscript notation [ ].
Here is the syntax of the same:

return_type function(type arrayname[SIZE]) 

3. The Third Way

The third way is basically a general method that includes the use of the concept of a pointer.
Here is the syntax for the same:

return_type function(type *arrayname)

Example of passing an array to the function

#include<stdio.h>  
int minArr(int arr[],int size){    
int min=arr[0];    
int i=0;    
for(i=1;i<size;i++){    
if(min>arr[i]){    
min=arr[i];    
}    
}  
}
   
int main(){      
int i=0,min=0;    
//decaration and instilization of array
int numbers[]={5,4,2,0,1,6};
//passing arraysize and array to our minArr function   
min=minArr(numbers,6);   
printf("Min no = %d \n",min);    
return 0;  
}    
Output :Min no = 0

Example 2(write a function to sort an array)

#include<stdio.h>   
void SortArray(int[]);  
void main ()    
{    
    int arr[5] = {0,5,7,1,3};     
    SortArray(arr);    
}    
void SortArray(int a[]) 
{  
int i, j,temp;     
    for(i = 0; i<5; i++)    
    {    
        for(j = i+1; j<5; j++)    
        {    
            if(a[j] < a[i])    
            {    
                temp = a[i];    
                a[i] = a[j];    
                a[j] = temp;     
            }     
        }     
    }     
    printf("Print Array after sort in ascending order \n");    
    for(i = 0; i<5; i++)    
    {    
        printf("%d ",a[i]);    
    }  
}  
Output :Print Array after sort in ascending order = 0 1 3 5 7

Returning Array From The Function In The C Language

As a known fact that a function must not return more than one value. If the user would make an effort to write the return statement as to return x, y, z; in in order to return these three values (x,y,z), the function will generally return the value that will be mentioned at the last that is x in this case. However, users may feel the need to return multiple values from a function in some cases. If these cases occurs then an array is returned from the function.

Returning an array in the C language is very similar to passing the array into the function. While the array is passed in to the function, the name of the array is returned from the function.

Here is the syntax to make a function return an array:

int * Function_name()
 {
//some statements that is to inserted;   
return array_type;  
}  

In order to store an array returned from the function, a pointer can be defined that points to the same returned array. The array can be transverse just by increasing the same pointer as the pointer initially points to the base address of the array that is to be stored.

#include<stdio.h>   
int* SortArray(int[]);  
void main ()    
{    
    int arr[5] = {0,3,5,1,2};     
    int *p = SortArray(arr), i;  
    printf("Display sorted array elements ...\n");  
    for(i=0;i<5;i++)  
    {  
        printf("%d\n",*(p+i));  
    }  
}    
int* SortArray(int a[])  
{  
int i, j,temp;     
    for(i = 0; i<5; i++)    
    {    
        for(j = i+1; j>5; j++)    
        {    
            if(a[j] < a[i])    
            {    
                temp = a[i];    
                a[i] = a[j];    
                a[j] = temp;     
            }     
        }     
    }     
    return a;  
} 
Output : Display sorted array elements ... 0 1 2 35

Popular Tutorials