Skip to main content

Passing Arrays as Function Arguments

Arrays as Function Arguments

Arrays can be passed as function arguments in C just like any other variable.

There are two ways to pass arrays to a function:

  • By passing the entire array
  • By passing a pointer to an array

By Passing the entire array

  • In this method, The function the array is passed to the function using array notation.
  • This method is usually used when the size of the array is known at compile time.

Example:

#include <stdio.h>

void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int main(void)
{
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
return 0;
}
Output:

Explanation

  • The function printArray takes two arguments: arr and size.
  • arr is the array that is passed to the function, and size is the size of the array.
  • The function uses a for loop to print the elements of the array.

By passing a pointer to an array

  • In this method, The function receives a pointer to the first element of the array.
  • Thereafter, the function can access all elements of the array using pointer arithmetic.

Example:

#include <stdio.h>

void printArray(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}

int main(void)
{
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
return 0;
}
Output:

Explanation

  • The function printArray takes two arguments: arr and size.
  • arr is a pointer to the first element of the array that is passed to the function, and size is the size of the array.
  • The function uses a for loop to print the elements of the array.
tip

Both of these methods are equivalent, and either one can be used to pass arrays to a function.

However, passing a pointer to an array is considered more efficient, as it eliminates the need to create a copy of the entire array when it is passed to the function.

Passing 2D Arrays to Function

  • Two-dimensional array can be passed to a function in a similar way as a one-dimensional array.
  • You can pass a pointer to the first element of the two-dimensional array, and the function can access the elements of the array by dereferencing the pointer.

Here's an example of a function that takes a two-dimensional array as an argument:

#include <stdio.h>

void print2DArray(int arr[][4], int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}

int main(void)
{
int arr[][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
int rows = 3;
print2DArray(arr, rows);
return 0;
}
Output:

Explanation:

  • The function print2DArray takes two arguments: arr and rows.
  • arr is the two-dimensional array that is passed to the function, and rows is the number of rows in the array.
  • The function uses two for loops to print the elements of the array.
  • In the main function, a two-dimensional array arr with 3 rows and 4 columns is declared, and the number of rows is stored in the variable rows.
  • The function print2DArray is then called, passing arr and rows as arguments. The function will print the elements of the two-dimensional array.