I want to sort a list of string. To parameter of qsort() function is a list of c[i]’s defined as so :
//N is the number of words
int N;
scanf("%d",&N);
//each c[i] is a word
char **c;
c = malloc(N*sizeof(char*));
for(i = 0; i < N; i++)
{
char *temp[100];
scanf("%s", &temp);
c[i]=strdup(temp);
}
But when I sort the list, the result is wrong.
Precisely to debug, I added this
for(i = 0; i < N; i++){
j=compare (c[0],c[i]);
printf("%d",j);
printf("%s",c[i]);
}
My input is ‘4;a;b;c;d’ and I get ‘ 0b-1c-1d1a’
Can anybody help about this?
This is incorrect:
tempis an array of100uninitialisedchar*pointers, there is no storage for achar[]. Change to:From you comment:
This should be:
From the C99 standard section 7.20.5.2 The qsort function:
Which means that the type of the arguments to the
compare()function arechar**, and therefore need to be dereferenced before performing a string comparision.See http://ideone.com/rUG89 for example.