My qsort seems to not be sorting anything. When I pass strings to the program it doesn't sort
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int cmpstr(const void *a, const void *b){
char const *aa = (char const *)a;
char const *bb = (char const *)b;
return strcmp(aa,bb);
}
int main(int argc, char * argv[]){
qsort(argv, argc, sizeof(*argv), cmpstr);
for(int i =0; i < argc; i++)
printf("%s\n",argv[i]);
}
The comparison function should look like
That is the function
qosrtpasses to the comparison function pointers to elements of the sorted array. In the given code the elements have the typechar *. Pointers to elements have the typechar **. So to get elements you need to dereference pointers of the typechar **.Here is a demonstration program. The following arguments are supplied to the program
Within the program I excluded the element
argv[0]because the corresponding string can be too long in some systems.argv[0]points to the program name.The program output is