Array to function in C
Array to function in C
We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the array in the function call. We have to pass the size of an array as a parameter. Size may or may not be required.
Exclusion of size can be done in ‘\0’ terminated character arrays, and size can be determined by checking the end of the string character. You can pass both one dimensional and multi-dimensional arrays into the function.
A single array element or an entire array itself can be passed into the function. This passing of an array can be done for both one dimensional as well as a multi-dimensional array.
Syntax:
function_name (array_name);
Passing one dimensional array:
- Passing individual array elements to a function is almost identical to passing variables to a function.
Eg:
#include <stdio.h> void display (int height1, int height2) { printf (“ %d \n” , height1); printf (“ %d \n” , height2); } int main() { int heightArray[] = {162, 168, 164, 165}; //passing index numbers to the get the output display (height1[2], height2[3]); return 0; }
Output:
164 165
- Passing an entire array to a function using pointer
Eg:
#include <stdio.h> float totalHeight (float height[]); int main() { float result, height[] = {162, 167, 164, 163}; //array containing heights of students is passed to totalHeight() result = totalHeight(height); printf (“Result = %.2f”, result); return 0; } Float totalHeight (float height[]) { float sum = 0.0; for (int i = 0; i < 4; ++i) { sum += height[i]; } return sum; }
Output:
Result = 41
To pass an array to a function, the name of the specific array will be passed as an argument, such as, in the above example, we pass result = totalHeight(height);
Function definition has [], in it. It informs the compiler that you are passing one dimensional array to a function.
In the above code, we can see the code snippet:
float totalHeight (float height[]) { … }
- Passing an array using a pointer
void printArray (int *arr, int size) { int i; printf (“Array elements are:”); for (i = 0; i < size; i++) { printf (“%d” , arr[i]); } } int main() { int arr[10]; printArray(arr, 10) //passes array directly to function printArray return 0; }
Passing multi-dimensional array:
The multi-dimensional array can be passed to a function in the same way as we pass a one dimensional array. There are two ways in which we can pass an array into a function.
- Passing an array directly to a function:
The simplest way to pass a multi-dimensional array is to pass it as we would for any other variables.
E.g.:
#include <stdio.h> #define row 3 #define column 3 void printArray (int arr[] [column]); // function declaration to print 2D array int main() { int arr [row][column] = { {2, 4, 8}, {1, 3, 5}, {6, 7, 9} }; printArray (arr); return 0; } void printArray (int arr [][column]) { int i, j; printf (“Elements in a matrix is : \n”); for ( i = 0; i < row; i++) { for ( j = 0; j < column; j++) { printf (“%d”, arr[i][j]); } printf (“\n”); } }
- Passing an array to a function using pointer:
Eg:
#include <stdio.h> #define row 3 #define column 3 void inputArray (int (*arr) [column]); //function declaration void printArray (int arr[] [column]); int main() { int arr [row][column]; inputArray (arr); //input elements into the matrix using the function printArray (arr); //print elements of the matrix using the function return 0; } void inputArray (int (*arr) [column]) { int i, j; printf (“ Enter two dimensional array elements: \n”); //input elements in 2D for (i = 0; i < row; i++) { for (j = 0; j <column; j++) { scanf (“%d”, (*(arr + i) +j)); } } } void printArray(int (*arr) [column]) { int i, j; printf (“Elements in an array: \n”); for (i = 0; i < row; i++) { for (j = 0; j < column; j++) { printf (“%d”, *(*(arr+i) +j)); } printf (“\n”); } }
There are three ways to pass an array to a function in the C programming language:
- Using blank subscript notation []
return-type function (type array-name[])
- Defining size in subscript notation []
return-type function (type array-name[size])
- Using pointer
return-type function (type *arrayname)
Advantages:
- The array is passed as a pointer; hence the memory of the array will not be copied. The function uses the memory of the same array passed to it and can change everything in the memory.
- It can move the memory management to a caller, making a function more general.
Disadvantages:
- In the C programming language, you can always pass an array to a function, however, you cannot return arrays from functions in the same way.
- It may require an argument other than the declared to communicate the array size.
- It misunderstands the argument in the documentation.
Passing an array to a function is like a simple and, any programmer familiar with the concept can quickly implement it. Although one must remember that an array passed to function uses the ‘pass by reference’ concept.