×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

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

Problem statement

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.

Function Declaration

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

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.

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 »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.