Array as Parameter

Array as Parameter

In this article, we are going to discuss Array as Parameter. Please read our previous article, where we discussed parameter passing methods. At the end of this article, you will understand how an array can be passed as a parameter and we also discuss more stuff related to the array.

The array as Parameter:

Let us understand this directly with an example. Please have a look at the following example. As you can see in the below code, the main function is having an array of size 5 and it is also initialized with five elements (2,4,6,8,10). The name of the array is A. Then we are calling a function i.e. fun by passing that array A and an integer number that represents the size of the array. The fun function is taking two parameters. The first parameter is an array i.e. B and for passing an array as a parameter we need to mention empty brackets [] and we should not give any size. The fun function doesn’t know the size of the array because the array actually belongs to the main function. So, we should also pass what is the size of the array and for that, the second parameter i.e. n is being used. So, this B is actually a pointer to an array. It is not an array itself, it’s a pointer to an array. Within the function, using a for loop, we are printing all the elements of the array.

#include <stdio.h>
void fun (int B[], int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf ("%d", B[i]);
    }
}

int main ()
{
   int A[5] = { 2, 4, 6, 8, 10 };
   fun (A, 5);
}
What parameter passing method used in the above example?

The point that you need to remember is an array is always passed by address not by the value both in C and C++ Language. That means the base address of the array A is given to the pointer i.e. to B. Here B is just a pointer and giving bracket means it is a pointer to an array. The second parameter is n and if you notice there is no ‘*’ then it is not called by address and there is no ‘&’ then it is not called by reference. That means it is the call by value like just a normal variable. So, there are two parameters one is passed by address and another one is passed by value.

Can we write * instead of []?

Yes, we can. Instead of writing brackets, even you can write ‘*’ there as shown in the below code. Here, B is an integer pointer that will be pointing to an array.

#include <stdio.h>
void fun (int *B, int n)
{
    int i;
    for (i = 0; i < n; i++)
    {
        printf ("%d", B[i]);
    }
}

int main ()
{
   int A[5] = { 2, 4, 6, 8, 10 };
   fun (A, 5);
}
What is the difference between *B and B[]?

The difference is the *B is an integer pointer that can point to an integer variable as well as it can also point to an integer array. On the other hand, B[] is a pointer that can only point to an integer array and cannot point to an integer variable.

One more point that you need to understand, within the fun function, if you do any modification to the array then it will reflect the same in the main function as the array uses the pass by address mechanism. Let us understand this with an example.

#include <stdio.h>
void fun (int *B)
{
    B[0] = 20;
    B[2] = 30;
}

int main ()
{
    int A[5] = { 2, 4, 6, 8, 10 };
    fun (A);
    for (int i = 0; i < 5; i++)
    {
        printf ("%d ", A[i]);
    }
}
Returning an Array from a Method:

The C programming language does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array’s name without an index.

In order to understand this, please have a look at the below code. As you can see in the below code, inside the main function we have a pointer variable *A. Then the main function calling the fun function by passing a value 5. The fun function which is taking parameter ‘n’ will store the incoming value 5 and this is passed by the value mechanism. The fun function is also having a pointer variable i.e. *p and it is allocating an array of size of type integer * 5 in the heap area. We already discussed malloc function allocates memory in the heap area. In the heap area, it creates an array with size five and stores the base address of that array in the integer pointer i.e. *p.

#include <stdio.h>
int* fun(int n)
{
    int *p;
    p = (int *) malloc (n * sizeof (int));
    return (p);
}

int main ()
{
    int *A;
    A = fun (5);
}
How does it work?

The program execution starts from the main method. The main method first creates an integer pointer variable. The integer pointer variable can point to normal variables as well as to an array. Then it is calling the function fun() by passing 5 as the value.

The fun function is taking a parameter n and the value 5 will store in it. Then the malloc() function will allocate the memory in the heap and inside the heap, an array of size 5 will be created. The address of that array will be present in ‘p’. And after allocating the memory in the heap and store the base address in point variable p, it returns that pointer variable i.e. the base address of the array whose memory is allocated in the heap.

Inside the main function, now the pointer variable i.e. A will point to the array which is created in the heap memory. For better understanding please have a look at the below image.

Array as Parameter

In the next article, I am going to discuss the Structure as Parameter. Here, in this article, I try to explain Array as Parameter and I hope you enjoy this Array as Parameter article.

Leave a Reply

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