ADVERTISEMENT
ADVERTISEMENT

C program to pass a one dimensional (1D) array to a function

In this C program, we are going to learn how to pass a one dimensional array (One-D array/1D Array) to a function? Here, we are designing a user define function, in which we are passing an integer array.
Submitted by IncludeHelp, on March 20, 2018

Given a one dimensional array (an integer array) and we have to pass the array to a function and print the elements using C program.

Here is the function that we have used in the program,

void arrayfun(int*ptr , int count)

Here,

  • void is the returns type of the function i.e. this function does not return any value.
  • int *ptr is an integer pointer that will store the based address of the integer array which will be passed through the main() function.
  • int count is an integer variable that will store the total number of elements which will be passed through the main() function.

Function calling statement,

arrayfun(array,9);

Here,

  1. arrayfun is the name of the function.
  2. array is an integer array (one dimensional array).
  3. 9 is the total number of elements.
ADVERTISEMENT

Program to pass a one dimensional array to function in C

/*  
C program to pass 1D array to a function
*/

#include <stdio.h>

// function to print 1D array elements
void arrayfun(int*ptr , int count)
{
    int i=0;
    
    for(i=0;i<count;i++)
    {
        printf("\nArray [%d] : %d",i,ptr[i]);  fflush(stdout);
    }
    
}

// main function
int main()
{
	// Declare a buffer of type "char"
	int array[10] = {1,2,3,4,5,6,7,8,9};
	
	// Passing 1D-array to Function
	arrayfun(array,9);
	return 0;
}

Output

Array [0] : 1
Array [1] : 2
Array [2] : 3
Array [3] : 4
Array [4] : 5
Array [5] : 6
Array [6] : 7
Array [7] : 8
Array [8] : 9

C User-defined Functions Programs »


ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.