Passing Array Elements to a Function – C Programming

Arrays are passed to the functions as arguments. Like variables, a whole array can be passed to a function. We already know that functions make it so easy for us to manage complex programs and array makes it easy to handle a large number of data. So with this functionality, we can achieve greater simplicity in writing codes and execute them. An array can be passed to the function as an argument of the formal parameter in these ways:

  • Array with unspecified size: The array in the formal parameter has its characteristic parentheses but does not contain the size of the array in it. Example:
     
    void access(int num[])//the array doesn't have its size specified 
    
  • Array with specified size: The array in the formal parameter(as an argument) contains its size (It has no impact on the way you code, because even if you send an array with different size then also it will not give any error, it will be treated as no size array. So, one use that it could have is for the documentation purpose). Example:
     
    void access(int num[5])//the array has its size specified 
    

    Instead, one should use static with the size which tells the function that it is expecting a minimum number of mentioned elements in the array and hence it will optimize the execution accordingly. Example:

     
    void access(int num[static 5])//the array has its static size specified 
    
  • Array pointer: The formal parameter argument takes the value specified by the pointer. Example:
     
    void access(int *num1)//num1 is the pointer variable that points to the elements present in the specified memory location
    

Arrays can be passed to the functions in many manners. Let’s have a look at all those different ways and their classification based on their dimensions.


Single Dimensional Arrays

There are two ways of passing an array to a function – by value and by reference, wherein we pass values of the array and the addresses of the array elements respectively.
By value is a very direct process in which we pass the array elements directly to the function.
By reference makes use of pointers which indicates the address of the first array element and through that the array elements are accessed. Let’s have a look at both these processes:

By value

Here is a simple program that shows us the pass by value process. This program displays the elements of an array:

#include <stdio.h>
void access(int[]); // declaring an array function. Parentheses indicates that
                    // an array shall be passed in the function
int main() {
  int a[5], i;
  printf("Enter 5 elements in the array:\n");
  for (i = 0; i < 5; i++) {
    scanf("%d", &a[i]);
  }
  access(a); // here we are passing the whole array a and not individual
             // elements
  return 0;
}
void access(int a[5]) // the formal parameter has a sized array
{
  int i;
  printf("The array elements are as follows:\n");
  for (i = 0; i < 5; i++)
    printf("%d\n", a[i]); // displaying all the elements in the array
}

Output:-
Enter 5 elements in the array:
9
4
7
0
1
The array elements are as follows:
9
4
7
0
1

By reference

The following program shows us the pass by reference process of passing arrays into functions.This program displays the elements of an array:

#include <stdio.h>
void access(int *);
/*declaring an array function. parentheses indicates that we shall be passing
 * the whole array. is the pointing operator that receives the value of the
 * address passed to the function.*/
int main() {
  int i, x[5];
  printf("Enter 5 numbers in an array: \n");
  for (i = 0; i < 5; i++)
    scanf("%d", &x[i]);
  access(x); // here we pass the address of the array to the function access
  return 0;
}
void access(
    int *a) // we receive the value in another array variable with the name 'a'
{
  int i;
  printf("The array elements are as follows:\n");
  for (i = 0; i < 5; i++)
    printf("%d \n", a[i]); // displays the array elements
}

Output:-
Enter 5 elements in the array:
9
4
7
0
1
The array elements are as follows:
9
4
7
0
1

Multi-Dimensional Arrays

Multi-dimensional arrays are passed to a function in the same way as a single dimensional array. The only difference is that the number of parentheses while declaring a function will increase according to the dimension of the array.Like 1D arrays, the 2D array can also be passed by two methods: by value and by reference. Let’s have a look at these processes:

By value

Here is a program which shows how a multi-dimensional array(2D array here) is passed to a function. This program displays the elements of an array:

#include <stdio.h>
void display(int a[][2]); // declaring a function which takes a two dimensional
                          // integer array as an argument
int main() {
  int num[2][2], i, j;
  printf("Enter 2x2 numbers:\n");
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++) {
      scanf("%d", &num[i][j]);
    }
  }
  display(num); // the whole 2D array gets passed to the function display()
  return 0;
}
void display(int a[][2]) // the formal parameter takes an array as argument.
                         // Only first dimension is skippable
{
  int i, j;
  printf("The matrix is:\n"); // the matrix is displayed using two for loops
  for (i = 0; i < 2; i++) {
    for (j = 0; j < 2; j++)
      printf("%d ", a[i][j]);
    printf("\n");
  }
}

Output:-
Enter 2x2 numbers:
5
0
1
8
The matrix is:
5 0
1 8

By reference

The program below shows how we pass the array by reference with the help of pointers. It displays all the elements of a 2D array:

#include <stdio.h>
#include <stdlib.h>
void print(int **arr, int row, int col) {
  int i, j;
  printf("\nThe matrix is:\n");

  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++)
      printf("%d ", arr[i][j]);
    printf("\n");
  }
}
int main() {
  int row, col, i, j;

  printf("Enter number of rows - ");
  scanf("%d", &row);
  printf("\nEnter number of columns - ");
  scanf("%d", &col);
  int **arr = (int **)malloc(sizeof(int *) * row);
  for (i = 0; i < row; i++) {
    arr[i] = (int *)malloc(sizeof(int) * col);
  }
  printf("\nEnter %d * %d array\n", row, col);
  // now Scan or initialise
  for (i = 0; i < row; i++) {
    for (j = 0; j < col; j++) {
      scanf("%d", &arr[i][j]);
    }
  }
  // pass the array to function print as
  print(arr, row, col);
  return 0;
}

Output:-
Enter number of rows - 2
Enter number of columns - 3
Enter 2 * 4 array
5
1
2
1
9
0
The matrix is:
5 1 2 
1 9 0 

Passing single array elements to a Function

Passing single elements of an array to a function is very much like passing variables to an array. The following program shows how it is done. This program displays the elements of an array:


#include <stdio.h>
void display(int); // declaration of a function with a single integer argument
int main() {
  int i, num[5]; // taking an array of size 5
  printf("Enter 5 elements in an array:\n");
  for (i = 0; i < 5; i++) {
    scanf("%d", &num[i]);
  }
  printf("The array is:\n");
  for (i = 0; i < 5; i++) {
    display(num[i]); // a single element is being sent to the function to get
                     // displayed
  }
  return 0;
}
void display(
    int x) // formal parameter takes a single variable and not a whole array
{
  printf("%d\n", x); // displaying elements of the array
}


Output:-
Enter 5 elements in an array:
3
9
0
1
5
The array is:
3
9
0
1
5

Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.

Do not forget to share and Subscribe.

Keep Learning… Happy Learning.. 🙂

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index