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


Programming in C: Unit III (b): Pointers



Under Subject


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation



Related Subjects


Professional English II

HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Statistics and Numerical Methods

MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation


Engineering Graphics

GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation


Physics for Electrical Engineering

PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Basic Civil and Mechanical Engineering

BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Electric Circuit Analysis

EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation


Physics for Electronics Engineering

PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Electrical and Instrumentation Engineering

BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Circuit Analysis

EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation


Materials Science

PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation


Physics for Civil Engineering

PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Basic Electrical, Electronics and Instrumentation Engineering

BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation


Physics for Information Science

PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation


Basic Electrical and Electronics Engineering

BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation


Programming in C

CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation