Tutorial 8.4: Passing Arrays to Functions

An array can be passed as an argument to a function. Remember the mechanism of call-by-value. When we pass variables to a function, copy of their values are made for the function. Therefore any changes to the copy do not affect the original variables' value. But in the case of passing an array to a function, we are actually passing the memory location of the array which is its address. It is because the name of arrays evaluates to the address of the first element of the array. Thus, the called function can modifies the actual values in the original array since it knows exactly where the array is stored in the memory. The following program demonstrates that an array name is actually the address of its first element.
#include <stdio.h>

int main()
{
 int a[5] = {1, 2, 3, 4, 5};
 printf("&a[0] = %p\n", &a[0]);
 printf("   &a = %p\n", &a);
 printf("    a = %p\n", a);
 return 0;
}
Sample output:

&a[0] = 0022FF20
   &a = 0022FF20
    a = 0022FF20


Printing the address of the array's first element and its name yields the same output. The %p is a conversion specifier for printing address. As usual we will look at an example on how to pass an array to a function. It also demonstrates that a function can modify the elements in an array.
#include <stdio.h>

#define SIZE 5

/* function prototype */
void squareArray(int arr[], int size);

int main()
{
 int a[SIZE] = {1, 2, 3, 4, 5};
 int i;
 for(i = 0; i < 5; i++) printf("%d\t", a[i]);
 printf("\n");
 squareArray(a, SIZE);
 for(i = 0; i < 5; i++) printf("%d\t", a[i]);
 printf("\n");
 
 return 0;
}

/* function definition of squareArray */
void squareArray(int arr[], int size)
{
 int i;
 for(i = 0; i < size; i++) {
  arr[i] *= arr[i];
 }
}
Sample output:

1    2    3     4    5
1    4    9    16    25

Explanation:

First, an integer array a which contains five elements is declared.
 for(i = 0; i < 5; i++) printf("%d\t", a[i]);
 printf("\n");
Then we use for repetition statement to display the elements of the array. \t is an escape sequence that is used to insert horizontal tab. Once all the elements are displayed, a newline escape sequence is outputted to position the cursor at the beginning of the next line.
 squareArray(a, SIZE);
Next function squareArray is invoked. The array a and its size are passed as the arguments to the called function.

Finally we re-display the elements of the array to see changes made by function squareArray.

The following is the function definition of squareArray.
void squareArray(int arr[], int size)
{
 int i;
 for(i = 0; i < size; i++) {
  arr[i] *= arr[i];
 }
}
The function expects two arguments, an integer array and integer variable. It does not returns any value to the caller. The definition consists of integer variable i declaration and for repetition statement.

 for(i = 0; i < size; i++) {
  arr[i] *= arr[i];
 }
The for statement repeats for the number of elements (size of the array) and performs square operation on each element of the array.

3 comments:

Fuad said...

thanks...

Unknown said...
This comment has been removed by a blog administrator.
Unknown said...
This comment has been removed by a blog administrator.

Post a Comment