
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to send an entire array as an argument in C language?
An array is a group of related items that is stored with a common name.
Declaring array
The syntax for declaring an array is as follows −
datatype array_name [size];
Initialization
An array can be initialized in two ways, which are as follows −
- Compile time initialization.
- Runtime initialization.
An array can also be initialized at the time of declaration as follows −
int a[5] = {100,200,300,400,500};
Function
A function is a self-contained block that carries out a specific well-defined task. The two ways of passing the arrays as arguments to functions are as follows −
Sending an entire array as an argument to function.
Sending the individual elements as argument to function.
Now, let us understand how to send an entire array as an argument to function in C language.
Sending an entire array as argument to function
To send an entire array as argument, try to send the array name in the function call.
To receive an entire array, an array must be declared in the function header.
Example 1
Refer an example given below −
#include<stdio.h> main ( ){ void display (int a[5]); int a[5], i; clrscr( ); printf ("enter 5 elements"); for (i=0; i<5; i++) scanf("%d", &a[i]); display (a); // Sending entire array ‘a’ using array name getch( ); } void display (int a[5]) {//receiving entire array int i; printf ("elements of the array are"); for (i=0; i<5; i++) printf("%d ", a[i]); }
Output
When the above code is compiled together and executed, it produces the following result −
Enter 5 elements 10 20 30 40 50 Elements of the array are 10 20 30 40 50
Example 2
Following is the C program to print the elements in a reverse order from the array −
#include<stdio.h> void main(){ //Declaring the array - run time// int array[5],i; void rev(int array[5]); //Reading elements into the array// printf("Enter elements into the array:
"); //For loop// for(i=0;i<5;i++){ //Reading User I/p// printf("array[%d] :",i); scanf("%d",&array[i]); } //Displaying reverse order of elements in the array// printf("The elements from the array displayed in the reverse order are :
"); rev(array); // Sending entire array ‘a’ using array name getch(); } void rev(int array[5]){ //receiving entire array int i; for(i=4;i>=0;i--){ //Displaying O/p// printf("array[%d] :",i); printf("%d
",array[i]); } }
Output
When the above program is compiled together and executed, it produces the following result −
Enter elements into the array: array[0] :23 array[1] :34 array[2] :12 array[3] :56 array[4] :12 The elements from the array displayed in the reverse order are: array[4] :12 array[3] :56 array[2] :12 array[1] :34 array[0] :23
- Related Articles
- How to pass entire array as an argument to a function in C language?
- How to send individual elements as an argument in C language?
- How to pass entire structure as an argument to function in C language?
- How to pass an entire structure as an argument to function in C?
- How to pass individual elements in an array as argument to function in C language?
- How to pass the address of structure as an argument to function in C language?
- How to access an array element in C language?
- How to push a Lua table as an argument?
- How to pass the address of structure as an argument to function in C?
- Inserting elements in an array using C Language
- Alternatives to Lua as an Embedded Language
- What is an array of structures in C language?
- Can we pass objects as an argument in Java?
- How to find minimum element in an array using linear search in C language?
- How to find minimum element in an array using binary search in C language?
