So I am a complete beginner to C, and I was trying to sort an array of strings without using qsort. Here is the code:
#include <stdio.h>
#include <string.h>
void sort(char *ar[],int n)
{
int i,j;
char temp[10];
for (i=0;i<n-1;i++)
{
for (j=0;j<n-i-1;j++)
{
if ((strcmp (*(ar+j),*(ar+j+1)))>0)
{
strcpy (temp, *(ar+j));
strcpy (*(ar+j), *(ar+j+1));
strcpy (*(ar+j+1), temp);
printf ("%s\n", temp);
}
}
}
/*printf ("After sorting: \n");
for (i=0;i<n;i++)
printf ("%s\n", *(ar));*/
}
int main()
{
int i,n;
char* ar[]={"ghi","def","abc"};
n = sizeof(ar)/sizeof(*ar);
printf ("Before sorting: \n");
for (i=0;i<n;i++)
printf ("%s\n", *(ar+i));
sort (ar,n);
printf ("After sorting: \n");
for (i=0;i<n;i++)
printf ("%s\n", *(ar+i));
return 0;
}
However, it only prints the strings before sorting. What am I doing wrong here?