Different ways of passing an Array as a Parameter to a Function

Different ways of passing arrays as parameters
Methods of passing arrays as parameters

 

 An array is a collection of similar data elements grouped under a single name. The elements of the array are stored in adjacent memory locations.

This article will look at different ways of passing arrays as parameters to a function.


1. Pointer to an Array method - In the Pointer to an Array method, we pass the address of the array (actual parameter) to an array pointer (formal parameter) in the function. An array pointer is a pointer that can explicitly point to an array.  Any changes made to the formal parameters will also be considered in the actual parameters.

Example -

#include<stdio.h>

//Function to insert elements into the array
void insertion(int a[],int n)
{
    printf("\nEnter the elements: ");
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

//Function to display the elements of the array
void displayed(int a[], int n)
{
    printf("\nDisplaying the elements: ");
    for(int i=0;i<n;i++)
    {
        if(i==n-1)
        {
            printf("%d.", a[i]);
        }
        else
        {
            printf("%d, ",a[i]);
        }
    }
}

int main()
{
    int n;
    printf("\nEnter the size of the array: ");
    scanf("%d",&n);
    //Declaring an array 'ar' of size 'n'
    int ar[n];
    insertion(ar,n);
    displayed(ar,n);
    return 0;
}

The following user-defined functions are created -

  • insertion(int variable_name[], int variable_name) - This function receives 2 parameters which are an array pointer (variable_name[]) and size of the array as parameters, and does not return any value. The purpose of this function is to insert elements into the array. Any changes made will be considered in the actual parameters.
  • displayed(int variable_name[], int variable_name) - This function receives 2 parameters which are an array pointer (variable_name[]) and size of the array as parameters, and does not return any value. The purpose of this function is to display the elements of the array.


2. Integer Pointer Method - In the Integer Pointer method, we pass the address of the array (actual parameter) to an integer pointer (formal parameter) in the function. An integer pointer is a pointer that can point to any integer, and also an array.  Any changes made to the formal parameters will also be considered in the actual parameters.

Example -

#include<stdio.h>

//Function to insert elements into the array
void insertion(int *a,int n)
{
    printf("\nEnter the elements: ");
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}

//Function to display the elements of the array
void displayed(int *a, int n)
{
    printf("\nDisplaying the elements: ");
    for(int i=0;i<n;i++)
    {
        if(i==n-1)
        {
            printf("%d.", a[i]);
        }
        else
        {
            printf("%d, ",a[i]);
        }
    }
}

int main()
{
    int n;
    printf("\nEnter the size of the array: ");
    scanf("%d",&n);
    //Declaring an array 'ar' of size 'n'
    int ar[n];
    insertion(ar,n);
    displayed(ar,n);
    return 0;
}

The following user-defined functions are created -

  • insertion(int variable_name[], int variable_name) - This function receives 2 parameters which are an integer pointer (*variable_name) and size of the array as parameters, and does not return any value. The purpose of this function is to insert elements into the array. Any changes made will be considered in the actual parameters.
  • displayed(int variable_name[], int variable_name) - This function receives 2 parameters which are an integer pointer (*variable_name[]) and size of the array as parameters, and does not return any value. The purpose of this function is to display the elements of the array.

💡Note

  • The above-discussed methods all belong to the Call by Address method. Also, remember that we cannot pass arrays using Call by value address.
  • In the above examples, while calling the functions from the main(), for example, insertion(array_name, variable_name), for the actual parameter of passing the array address, you can also pass the address of the first element of the array i.e. &array_name[0] as both array_name and &array_name[0] pass the same value.


Output - Even though the working of the above 2 methods is different, their output remains the same.

Different methods of Passing Arrays as parameters
Sample Output



Thanks for reading. If you have any doubts or suggestions for improving the article, please mention them in the comments.

Post a Comment

0 Comments