Pass an Array to a C Function – 3 Easy Ways!

Filed Under: C Programming
Pass An Array To A C Function

Hey, folks! In this article, we will be focusing on 3 Easy ways to Pass an Array to a C Function. So, let us begin!!


What is a C Array?

Arrays in C, is a data structure that stores similar type of data values altogether at some static memory locations.

Arrays can be used alongside functions to deal and manipulate various data elements together.

So, let us now have a look at each of the three techniques to pass an array to a function in the upcoming section.


Technique 1: Pass an Array to a C function through call by value

In this technique, we would be passing the array to the function through call by value method.

In call by value, the value of the actual parameters are copied to the function’s formal parameter list. Thus, the changes made within the function do not reflect in the actual parameter list of the calling function.

Let us have a look at the below example to get a clear idea about this technique.

#include <stdio.h>
void show_value(int a)
{
   printf("%d ", a);
}
int main()
{
   int a[] = {10,20,30,40,50};
   printf("The elements of Array:\n");
   for (int i=0; i<5; i++)
   {
       show_value (a[i]);
   }

   return 0;
}

So, here we have passed every element of the array to the function ‘show_value’ through a for loop.

Output:

The elements of Array:
10 20 30 40 50 

Technique 2: Pass an Array to a C function through call by reference

In call be reference technique, we pass the address of the arguments as parameters. That is, both the actual and formal arguments refer to the same location.

Thus, any changes made inside the function are reflected in the actual parameters as well. So, let us now understand the implementation through the below example:

#include <stdio.h>
void show_value(int *a)
{
   printf("%d ", *a);
}

int main()
{
   int a[] = {10,20,30,40,50};
   printf("The elements of Array:\n");
   for (int i=0; i<5; i++)
   {
       show_value (&a[i]);
   }

   return 0;
}

Here, we pass the address of every element present in the array as ‘&array[element]’.

Output:

The elements of Array:
10 20 30 40 50 

Technique 3: Pass entire array to the function directly!

In the above examples, we have passed the array to the function in an element wise fashion. That is, the elements are passed one by one.

Now, we have passed the entire array to the function directly as shown below–

#include <stdio.h>
void print_arr( int *arr, int size)
{
    for(int i=1; i<=size; i++)
    {
        printf("Element [%d] of array: %d \n", i, *arr);
        arr++;
    }
}

int main()
{
     int arr[] = {10,20,30,40,50};
     print_arr(arr,5);
     return 0;
}

Here, we have passed entire array and its size to the function.

Further, we print every element of the array using the for loop and the statement arr++ enables us to increment the pointer and refer to the next element of the array until the condition is met.

Output:

Element [1] of array: 10 
Element [2] of array: 20 
Element [3] of array: 30 
Element [4] of array: 40 
Element [5] of array: 50 

Conclusion

By this, we have come to the end of this topic. Hope this article helps you to implement the concept well!

Feel free to comment below, in case you come across any question.

Till then, Stay tuned @ C programming with JournalDev and Keep Learning!!

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages