i am trying to sort an array, using pointers and also without using index variables
void sort(int *a,int n)
{
int *temp;
*temp=0;
int *b=a;
for(;a<a+n-1;a+=1)
{
for(b=a+1;b<b+n;b+=1)
{
if(*a>*b)
{
*temp=*a;
*a=*b;
*b=*temp;
}
}
}
}
the equivalent version of the above program without using pointers is
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
i am doing an exercise question, and i just started learning the pointers by myself. i believe i am doing it right as i am trying to avoid the usage of index variables too. i am receiving an error segmentation fault : 11
. what am i missing here? is the program wrong in someway? or the idea itself is wrong? feedback appreciated.
a<a+n-1
- that's always true if n
> 1.
BTW, the argument int a[]
decays to a pointer.