Passing arrays to functions in C Language

Passing-Arrays-to-functions-in-C-Language

Introduction:

We looked at the one-dimensional arrays and two-dimensional arrays in our earlier posts, In today’s article, we are going to look at passing arrays to functions in C programming language with example programs.

A function is a basic concept in the C programming language that is a block of code that performs a specific task. One of the primary benefits of using functions is that they allow users to write modular and reusable code, which can make your programs more efficient and manageable.

Prerequisites:

This tutorial is part of the Arrays tutorials series.

Passing Arrays to Functions in C:

The arrays are passed by address( &) in the C language. So any changes made inside the function will reflect on the caller.

The arrays are similar to the other datatype variables( int, float, etc), so We can also pass them to functions. We can pass the whole array to function or individual array elements. We can pass multi-dimension arrays to functions as well.

📢 Arrays are passed by address, So any changes made in the called function will reflect on the caller function.

Passing a One-dimensional Array to Function in C:

Let’s look at how we can pass a one-dimensional array to function in the C programming language.

Function Call (Actual Arguments):

The C Array name holds the address of the first array element( arr[0]).

  • So to pass an array to a function, We can simply specify the array name in the actual arguments like below
    • func(array_name, size);
  • We can also explicitly use the address of operator(&) in the function call like below
    • func(&array_name, array_size);

The above two cases are valid.

When we pass an array to a function, the function has no idea how big the array is, so it can’t tell how many elements are in it. To allow the function to access the entire array, we need to let the function know the size of the array. This can be accomplished in various ways, but one common method is passing the array’s size as a separate argument to the function. That is the reason we are specifying the array_size in the above function calls.

Function Definition ( Formal Arguments ):

There are several syntaxes for formal arguments to receive the array. Let us examine them.

  • Array as the Formal Argument
  • Pointer as the Formal Argument

The above two methods are valid and they both accept the array address. So any changes we made inside the called function will reflect on the caller. So we need to be careful while modifying the array elements.

Passing arrays to functions in C – Array as the Formal Argument:

Here is the syntax of the array as the formal argument.

In the above code, We used array_name[] array. Let’s look at the program to understand Passing arrays to functions in C language.

Example to understand Passing an arrays to function:

C Program to create a function to print the elements of the passed array.

Program Algorithm:

  • The program defines a function display(). The display function takes in an integer array arr[] and its size size as arguments. The display() function doesn’t return any value – void display(int arr[], int size);
  • The display function then iterates through all the elements in the array and prints each element along with its index to the standard output. The display function uses two for loops to print the elements.
  • We created an array arr of size ARRAY_SIZE and initialized with {0, 11, 22, 33, 44, 55, 66, 77, 88, 99} values in the main function.
  • Then the display() function is then called with arr and ARRAY_SIZE as arguments – display(arr, ARRAY_SIZE);
  • The display() function will print the elements of arr to the standard output.
  • We also used a Macro in the program. The #define ARRAY_SIZE 10 line is a preprocessor directive that defines a macro named ARRAY_SIZE with the value 10. The macro is then used as the size of the arr array in the main() function.

Program Output:

Compile and Run the program using your compiler.

passing-arrays-to-function-as-array-in-c-program-output

As you can see from the above output, The program displayed all elements of the 2-D array with row and column indices.

Passing arrays to functions in C – Pointer as the Formal Argument:

Here is a program to read input from the user and print it using functions.

Example to understand Passing an array to function (Pointer):

Program Algorithm:

  1. Create a macro named ARRAY_SIZE with the size of the array.
  2. In the main() function, declare an integer array arr with ARRAY_SIZE elements.
  3. Call the readInput() function from the main() function and pass the arr and its size as arguments. The readInput() function will prompt the user for input values for each element of the array and update the array.
    1. The readInput() function takes an integer pointer arr and its size as formal arguments – void readInput(int *arr, int size)
    2. It then prompts the user for input values for each element of the array( arr).
    3. The values are read using scanf() function and stored in the corresponding array( arr) element.
  4. Call the display() function and pass the arr and its size as arguments. The display() function will print the elements of the array to the console.
    1. The display function also takes an integer pointer arr and size as formal arguments – void display(int *arr, int size)
    2. Then it uses a for loop to iterate over each element of the arr , it prints the element’s index and its corresponding value.

Program Output:

Let’s compile and run the program.

passing-array-to-function-as-pointer-program-output

Passing Two-dimensional Arrays (2D arrays) to Function in C:

We use the array or pointer (formal arguments) to pass the two-dimensional array to function.

If we are using the array as a formal argument, Then we must specify the second dimension, have look at below syntax

The columns is mandatory as the compiler needs to know how many elements the array contains.

let’s look at an example program to pass a 2-D array to function.

Here we have created a print function and passed the arr 2D array to print function, The print function accepts the arr 2-D array and prints its elements.

Program Output:

Compile and run the program.

passing-2d-array-to-function-accept-as-array

Different ways to Pass 2d arrays to functions in C:

Here are a few other syntaxes we can use to pass the 2-D array to function (Accepting as an 2D array).

Pointer as formal arguments

Conclusion:

We looked at how to pass an array to a function, What are the different ways we can pass the array to a function with example programs. We also covered how to pass the multidimensional array or 2-d array to function.

Tutorials Index:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

12 Responses

  1. […] How to pass arrays to functions in C […]

  2. […] How to pass arrays to functions in C […]

  3. […] 📢Related Article: Passing Arrays to Functions in C […]

  4. […] better understand the following program, Please go through the C Arrays, Functions in C, and How to Pass Arrays to Functions […]

  5. […] How to pass arrays to functions in C […]

  6. […] How to pass arrays to functions in C […]

  7. […] Passing Arrays to the functions in C programming […]

  8. […] How to pass arrays to functions in C […]

  9. […] How to pass arrays to functions in C […]

  10. […] Passing Arrays to Functions in C […]

  11. […] How to pass Arrays to the Functions […]

  12. […] Passing 2D Arrays to Functions […]

Leave a Reply