Passing arrays to functions:-
Arrays are passed to a function by reference by default. Arrays can be passed as an argument to a function using any 3 following ways. They are,
1. By declaring formal argument/parameter as a pointer variable.
Example :
Aread(p)
int *p;
{
}
2. By declaring formal argument as an unsized array.
Example
Aread(a)
int a[];
{
}
3. By declaring formal argument as sized array.
Example
Aread(a)
int a[10];
{
}
Following Example depicts about the different ways of passing arguments a function,
#include
ArrayRead(p,n)
int *p,n;
{
int i;
printf("\nEnter the array elements ");
for(i=0;i{
scanf("%d",*(p+i));
}
}
ArrayPrint(a,n)
int a[],n;
{
int i;
printf("\nThe array elements ");
for(i=0;i{
printf("%3d",a[i]);
}
}
Sort(a,n)
int a[50],n;
{
int i,j,t;
for(i=0;i {
for(j=i+1;j {
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
#include
main()
{
int a[50],n;
printf("\nEnter the size of array ");
scanf("%d",&n);
ArrayRead(a,n);
ArrayPrint(a,n);
ArraySort(a,n);
}
Arrays are passed to a function by reference by default. Arrays can be passed as an argument to a function using any 3 following ways. They are,
1. By declaring formal argument/parameter as a pointer variable.
Example :
Aread(p)
int *p;
{
}
2. By declaring formal argument as an unsized array.
Example
Aread(a)
int a[];
{
}
3. By declaring formal argument as sized array.
Example
Aread(a)
int a[10];
{
}
Following Example depicts about the different ways of passing arguments a function,
#include
ArrayRead(p,n)
int *p,n;
{
int i;
printf("\nEnter the array elements ");
for(i=0;i
scanf("%d",*(p+i));
}
}
ArrayPrint(a,n)
int a[],n;
{
int i;
printf("\nThe array elements ");
for(i=0;i
printf("%3d",a[i]);
}
}
Sort(a,n)
int a[50],n;
{
int i,j,t;
for(i=0;i
for(j=i+1;j
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
#include
main()
{
int a[50],n;
printf("\nEnter the size of array ");
scanf("%d",&n);
ArrayRead(a,n);
ArrayPrint(a,n);
ArraySort(a,n);
}
Comments
Post a Comment