Programming in C: Unit III (b): Pointers

Passing an Array to Functions

with Example C Programs | Pointers

An array can be passed to a function using pointers. For this, a function that expects an array can declare the formal parameter in either of the two following ways: func (int arr[]); OR func (int *arr);

PASSING AN ARRAY TO A FUNCTION

An array can be passed to a function using pointers. For this, a function that expects an array can declare the formal parameter in either of the two following ways:

func (int arr[]); OR func (int *arr);

When we pass the name of the array to a function, the address of the zeroth element of the array is copied to the local pointer variable in the function. Observe the difference; unlike ordinary variables the values of the elements are not copied, only the address of the first element is copied.

When a formal parameter is declared in a function header as an array, it is interpreted as a pointer to a variable and not as an array. With this pointer variable you can access all the elements of the array by using the expression, array_name + index. To find out how many elements are there in the array, you must pass the size of the array as another parameter to the function. So for a function that accepts an array as parameter, the declaration should be as follows.

func (int arr[], int n); OR func (int *arr, int n);

Look at the following program which illustrates the use of pointers to pass an array to a function.

21. Write a program to read and print an array of n numbers, then find out the smallest number. Also print the position of the smallest number.

#include <stdio.h>

void read_array (int *arr, int n);

void print_array(int *arr, int n);

void find_small (int *arr, int n, int *small, int *pos);

int main()

{

int num [10], n, small, pos;

printf("\n Enter the size of the array: ");

scanf("%d", &n);

read_array (num, n);

print_array (num, n)

find_small (num, n, &small, &pos);

printf("\n The smallest number in the array is %d at position %d", small pos);

return 0;

}

void read_array(int *arr, int n)

{

int i;

printf("\n Enter the array y elements: ");

for (i=0; i<n;i++)

scanf("%d", &arr[i]);

}

void print_array (int *arr, int n)

{

int i;

printf("\n The array elements are: ");

for (i=0;i<n;i++)

printf("\t %d", arr[i]);

}

void find_small (int *arr, int n, int *small, int *pos)

{

for (i=0;i<n;i++)

{

if (* (arr+i]) < *small)

{

* small = * (arr+i);

*pos = i;

}

}

}

Output

Enter the size of size of the array: 5

Enter the array elements: 1 2 3 4 5

The array elements are: 1 2 3 4 5

The smallest number in the array is 1 at position 0

It is not necessary to pass the whole array to a function. We can also pass a part of the array known as a sub-array. A pointer to a sub-array is also an array pointer. For example, if we want to send the array starting from the third element then we can pass the address of the third element and the size of the sub-array, i.e., if there are 10 elements in the array, and we want to pass the array starting from the third element, then only eight elements would be a part of the sub-array. So the function call can be written as

func (&arr [2], 8);

Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs | Pointers - Passing an Array to Functions