Programming in C: Unit II (a): Arrays

Passing Arrays to Functions

with Example C Programs

Like variables of other data types, we can also pass an array to a function. While in some situations, you may want to pass individual elements of the array, and in other situations you may want to pass the entire array.

PASSING ARRAYS TO FUNCTIONS

Like variables of other data types, we can also pass an array to a function. While in some situations, you may want to pass individual elements of the array, and in other situations you may want to pass the entire array. In this section, we will discuss both these cases. Look at Figure 5.21 which will make the concept easier to understand.

Passing Individual Elements

The individual elements of an array can be passed to a function by passing either their addresses or their data values.

Passing Data Values

The individual elements can be passed in the same manner as we pass variables of any other data type. The condition is just that the data type of the array element must match with the type of the function parameter. Figure 5.22 shows the code to pass an individual array element by passing data value.

In the example given, only one element of the array is passed to the called function. This is done by using the index expression. So arr [3] actually evaluates to a single integer value. The called function hardly bothers whether a normal integer variable is passed to it or an array value is passed.

Passing Addresses

Like ordinary variables, we can pass the address of an individual array element by preceding the indexed array element with the address operator (&). Therefore, to pass the address of the fourth element of the array to the called function, we will write &arr [3].

However, in the called function the value of the array element must be accessed using the indirection (*) operator (Figure 5.23).

Passing the Entire Array

We have studied that in C the array name refers to the first byte of the array in memory. The address of rest of the elements in the array can be calculated using the array name and the index value of the element. Therefore, when we need to pass an entire array to a function, we can simply pass the name of the array. Figure 5.24 illustrates the code which passes the entire array to the called function.

Programming Tip: When an entire array is to be sent to the called function, the calling function just needs to pass the name of the array.

In cases where we do not want the called function to make any changes to the array, the array must be received as a constant array by the called function. This prevents any type of unintentional modifications of the array elements. To declare the array as a constant array, simply add the keyword const before the data type of the array.

19. Write a program to read and print an array of n numbers.

#include <stdio.h>

#include <conio.h>

void read_array( int arr[], int);

void display_array( int arr[], int);

int main()

{

int num [10], n;

clrscr();

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

scanf("%d", &n);

read_array (num, n);

display_array (num, n);

getch();

return 0;

void read_array (int arr [10], int n)

{

int i;

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

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

{

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

}

}

void display_array (int arr [10], int n)

{

printf("\n The elements of the array are n");

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

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

Output

Enter the size of the array: 5

Enter the elements of the array: 1 2 3 4 5

The elements of the array are: 1 2 3 4 5

20. Write a program to merge two integer arrays. Also display the merged array in reverse order.

Programming Tip: While using arrays, we must check for validity of the index because such checks are not made by the compiler. An invalid index when used with an assignment operator may destroy the data of another part of the program and thus cause it to fail later. While initializing the array, if we provide more initializers than the number of elements in the array, a compiler error will be generated.

#include <stdio.h>

#include <conio.h>

void read_array(int my_array [], int);

void display_array (int my_ array[], int);

void merge_array(int my_array3 [], int , int my_arrayl [], int, int m my array2 [], int);

void reverse_array (int my_ array [], int);

int main()

{

int arr1 [10], arr2 [10], arr3 [20], n, m, t;

clrscr();

printf("\n Enter the number of elements in the first array: ");

scanf("%d", &m);

read_array (arr1, m);

printf("\n Enter the number of elements in the second array: ");

scanf("%d", &n);

read_array (arr2, n);

t = m + n;

merge_array (arr3, t, arrl, m, arr2, n);

printf("\n The merged array is : ");

display_array (arr3, t);

printf("\n The merged array in reverse order is: ");

reverse_array (arr3, t);

getch();

return 0;

}

void read_array (int my_array [10], int n)

{

int i;

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

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

}

void merge_array (int my_array3 [], int t, int my_array1 [], int m, int my_array2 [], int n)

{

int i, j=0;

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

{

my_array3 [j] = my_array1 [i];

j++;

}

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

{

my_array3 [j] = my_array2 [i];

j++;

}

}

void display_array (int my_array[], int n)

{

int i;

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

printf("\n Arr [%d] = %d", i, my_array[i]);

}

void reverse_array(int my_array[], int m)

{

int i, j;

for (i=m-1, j=0;i>=0; i--, j++) 

printf("\n arr[%d] = %d", j, my_array[i]);

}

Output

Enter the number of elements in the first array: 3

10 20 30

Enter the number of elements in the second array: 3

15 25 35

The merged array is:

Arr [0] = 10  Arr [1] = 20  Arr [2] = 30

Arr [3] = 15  Arr [4] = 25  Arr [5] = 35

The merged array in reverse order is:

Arr [0] = 35  Arr [1] = 25  Arr [2] = 15

Arr [3] = 30  Arr [4] = 20  Arr [5] = 10

21. Write a program to interchange the largest and the smallest number in an array.

#include <stdio.h>

#include <conio.h>

void read_array (int my_array [], int);

void display_array (int my_array[], int);

void interchange (int arr[], int);

int find_biggest_pos (int my_array [10], int n);

int find_smallest_pos (int my_array [10], int n);

int main()

{

int arr [10], n;

clrscr();

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

scanf("%d", &n);

read_array (arr, n);

printf("\n The elements of the array are\n");

display_array (arr, n);

interchange (arr, n);

printf("\n_The elements of the array after interchange are\n");

display_array (arr, n);

getch();

return 0;

}

void read_array (int my_array [10], int n)

{

int i;

printf("\n Enter the elements");

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

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

}

void display_array (int my_array [10], int n)

{

int i;

printf("n");

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

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

}

void interchange (int my_array [10], int n)

{

int temp, big_pos, small_pos;

big_pos = find_biggest_pos (my_arr, n);

small_pos = find_smallest_pos (my_arr, n);

temp = my_array [big_pos];

my_array [big_pos] = my_array [small_pos];

my_array[small_pos] = temp;

}

int find_biggest_pos (int my_array [10], int n)

{

int i, large = my_array [0], pos=0;

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

{

if (my_array[i]> large)

{

large = my_array[i];

pos=i;

}

}

return pos;

}

int find_smallest_pos (int my_array [10], int n)

{

int i, small = my_array[0], pos=0;

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

{

if (my_array [i] < small)

{

Small = my_array[i];

pos=i;

}

}

return pos;

}

Output

Enter the size of the array: 5

Enter the elements 1 2 3 4 5

The elements of the array are

Arr [0] = 1 Arr [1] = 2 Arr [2] = 3

Arr [3] = 4 Arr [4] = 5

The elements of the array after interchange are

Arr [0] = 5 Arr [1] = 2 Arr [2] = 3

Arr [3] = 4 Arr [4] = 1

Note

If a function receives an array that does not change it, then the array should be received as a constant array. This would ensure that the contents of the array are not accidentally changed. To declare an array as constant, prefix its type with the const keyword, as shown below. int sum(const int arr[], int n);

Programming in C: Unit II (a): Arrays : Tag: : with Example C Programs - Passing Arrays to Functions