×

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.


Related Topics

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Floyd’s triangle in C

Floyd’s triangle in C Floyd’s triangle is named after a person Robert Floyd. It is a right-angled triangle that is of natural numbers starting from 1 and consecutively selects the upcoming...

4 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Beep() function in C

Introduction: Beep is a function which is used in C programming language. The beep function uses to make a beep sound. In the C language, when we want to generate...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

What is String Comparison in C

String comparison is the process of comparing two strings (sequences of characters) to determine if they are equal, or if one is greater or less than the other. The comparison...

4 minutes read.