×

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

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

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 a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

memmove() in C

Introduction: In this article we are discuss about the memmove() function in C. This function transfers memory blocks from one location to another. The memmove() function is declared in the...

3 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Exponential() in C

Introduction: In C language, there are available many types of functions. The exponential() function is one of them. It is a mathematical function. This article describes using the pow() property to...

3 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 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.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.