Skip to content

Pass array to function in C example – By base pointer

Program to pass array to function in C with example. To pass the entire array, base address pointer of the array is passed to a function as an argument. Here is the syntax declaration of function and calling function.

function : int func(int *arr, int len)
function call: func(arr, len); or func(&arr, len);

In func(&arr, len), the address operator is optional and C program should work without any issue. By accessing base pointer of the array within the function, all elements can be access by index form 0 to len-1. Note that base pointer i.e. arr in above declaration cannot be increment. So, in a loop index will be increases to access all elements of the array.

Example to pass array to function C program

* Program for how to pass array to function in C
* example


//Receive array and print it.
int func(int *arr, int len) {  

	int i =0;
	for( i; i<len;++i)

int main(){

	int arr[] = {1, 2, 3, 4, 5};

	//Calculate the lenght of array using
	//sizeof operator
	int len = sizeof(arr)/sizeof(arr[0]);   
	//pass array and its length to function
	//arr is the base address of array
	func(arr, len);
	//it can also be passed using address
	//operator. However, this is optional
	func(&arr, len);
	return 0;

Published inC Programming Examples