Pass Array to Function in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

In C programming, arrays efficiently store and manage similar data. They can be passed to functions using pointers to their base address, enabling dynamic data handling in operations like sorting. This article discusses methods for passing and returning arrays in C.

Passing an Array to a Function in C

Contiguous Memory Locations

Every C function can have arguments passed to it in either of two ways:

  1. Pass by value
  2. Pass by reference

Since arrays are a continuous block of values we can pass the reference of the first memory block of our array to the function and then we can easily calculate the address of any element in the array using the formula -

This way we can easily pass an array to function in C by its reference.

Example: How Arrays are Passed in C?

In the example mentioned below, we have passed an array arr to a function that returns the maximum element present inside the array.

Output:

Methods to Pass an Array as an Argument

Arrays can be passed to function using either of two ways

  1. Passing array as a pointer variable
  2. Passing array as reference
    OR

Compiler breaks either of approaches to a pointer to base address of array i.e. int* array so passing int array[3] or int array[] or int* array breaks down to the same thing and to access any element of array compiler can find its value stored in location calculated using the formula stated above. This we we are passing the array to function in C as pass by reference.

C Function to Sort the Array

Output:

In this example we are passing array to function in C and then we are performing our sort inside the function. Because we have passed array by reference changes on array persists when program leaves the scope of the function.

Returning an Array from a Function

We can return an array from function in C using four ways

  1. Returning the array passed to function
  2. Returning dynamically created array
  3. Return array using static array
  4. Returning array using struct

For first three cases we can return the array by returning poiniter pointing to base address of the array.

1. Returing the array passed in function

2. Returning Dynamically Created Array

Dynamically create an array inside the function and then return a pointer to the base address of this array.

3. Return Array Using Static Array

We can create a static array that will make the array available throughout the program. Therefore, we can return the actual memory address of this static array.

4. Returning Array Using Struct

We can create our own data type using keyword struct in C which has array inside it and this data type can be returned from the function.

Pass Individual Array Elements

To pass individual elements of an array to a function, the array name along with its subscripts inside square brackets [] must be passed to function call which can be received in simple variables used in the function definition.

1. Example: Pass Individual Array Elements

2. Example: Pass Arrays to Functions

Output of both functions are same,

Pass Multidimensional Arrays to a Function

Let us understand how we can pass a multidimensional array to functions in C.

Pass Multidimensional Array to Functions in C

Passing 2-D array to functions

To pass a 2-D array in a function in C there is one thing we need to take care of that is we should pass the column size of the array along with the array name. So, we can pass the 2-D array in either of two ways

or

Example: Pass Two-dimensional Arrays

Output:

Why is it compulsory to pass column size in arguments?

To answer this we need to understand how 2-D arrays are arranged in memory. Just like a linear array, 2-D arrays are also stored in a contiguous arrangement that is one row after another as shown in the figure.

Pass Column Size in Arguments

So our previous formula to calculate the N^th^ element of an array will not work here. The new formula will be if an array is defined as arr[n][m] where n is the number of rows and m is the number of columns in array then,

As we can see from the above example for the compiler to know the address of arr[i][j] element it is important to have the column size of the array (m). This is the reason why passing int array[][] to the function will result in a compiler error.

So passing something like

will break down to int** array syntactically, it will not be an error but when you will try to access array[1][3] compiler will not be able to tell which element you want to access but if we pass it as array to function as

compiler will break this to something like int (*array)[4] and compiler can find the address of any element like array[1][3] which will be &array[0][0] + (1*4 + 4)*(sizeof(int)) because compiler knows second dimension (column size).

Example: Row Wise Sorting of a 2-D Array

Output

Pass 2-D array as a single pointer

We can also pass a 2-D array as a single pointer to function but in that case, we need to calculate the address on individual elements to access their values.

Output:

Similarly, to pass an array with more than one dimension to functions in C we can either pass all dimensions of the array or omit the first parameter and pass the remaining element to function, for example, to pass a 3-D array function will be

or

Example Demonstrating Passing Array as Reference

When we pass an array to functions by reference the changes which are made on the array persists after we leave the scope of function this can be demonstrated from this example-

Output:

Receiving array as a pointer variable

As we have discussed above array can be returned as a pointer pointing to the base address of the array and this pointer can be used to access all elements in the array. The below example demonstrates the same.

Output:

Advantages

  1. 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.
  2. As we pass reference of base address of the array this means 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.
  3. Because arrays are passed by reference to functions this prevents stack memory overflow in the case of recursive functions.

Disadvantages

  1. 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 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.
  2. If the memory space is more than elements in the array this leads to wastage of memory space.
  3. Special care is required when dealing with a multidimensional array as all the dimensions are required to be passed in function.

Conclusion

  • Passing Array to function in C can also be done by pointers and because they are passed by reference changes made on an array will also be reflected on the original array outside function scope.
  • Arrays can be returned from functions in C using a pointer pointing to the base address of the array or by creating user-defined data type using struct.
  • To pass a multidimensional array to function it is important to pass all dimensions of the array except the first dimension.
  • Because arrays are passed by reference it is faster as a new copy of the array is not created every time function is executed.