There are two program both work correct , can you explain me why?
//sorting of array using pointers
#define n 5
void sort(int m,int *x);
main()
{
int i,a[n];
printf("enter the values");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(n,a);//not sending the address but still my code works only when I define array but when i use int data type then I need to pass address , why is this so?
}
void sort(int m,int *x)
{
int i,j,temp,k;
for(i=1;i<=m-1;i++)
{
for(j=1;j<=m-1;j++)//this lop is to sort array
{
if(*(x+j-1)>=*(x+j))
{
temp=*(x+j-1);
*(x+j-1)=*(x+j);
*(x+j)=temp;
}
}
}
for(k=0;k<m;k++)
{
printf("\n%d\n",*(x));
x++;//when i am not using *x++
}
}
my progrm doest change or doesnt give error when i use this program
//sorting of arrays sing pointers
#define n 5
void sort(int m,int *x);
main()
{
int i,a[n];
printf("enter the values");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(n,&a);//and '&a' instead of 'a'
}
void sort(int m,int *x)
{
int i,j,temp,k;
for(i=1;i<=m-1;i++)
{
for(j=1;j<=m-1;j++)//this loop is to sort array
{
if(*(x+j-1)>=*(x+j))
{
temp=*(x+j-1);
*(x+j-1)=*(x+j);
*(x+j)=temp;
}
}
}
for(k=0;k<m;k++)
{
printf("\n%d\n",*(x));
*x++;//here i have used *x++ instead of x++
}
}
why is it necessary to pass adress in function call when we define int data type and we do not need to pass address in array when calling a function , when we are woring in pointers?
kindl clear my doubt.
main()should beint main(void), least. Where are theincludes?