Sunday 6 June 2021

Passing an array to function using pointers - call by reference

An array can be passed to a function using pointers. For this, a function that accepts an array can be declared as

Example: void display(int *arr, int n);;

In the above example, the function name is display and it accepts two arguments one is an array ( declared using pointer) and other is an integer variable which is used to pass index number of the array.

When we pass the name of the array through a function, the address of the first element of the array is copied to the local pointer variable in the function.

Example:

void display(int *arr, int n); // function decleraton 
void main()
{
int numbers[5]={10,20,30,40,50};
printf("\n enter n value");
scanf("%d",&n);
display(numbers,n);  // function calling
gtech()
}
void display(int *arr, int n)   // function calling
{
int i;
printf("\n array elements are");
for(i=0;i<n;i++)
printf("%d",arra[i]);
}


0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top