I tried sorting my environment.
My C++ version (the std::sort line along with the CstrLess class) works, but the qsort version fails. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
extern char** environ;
struct CstrLess{
bool operator()(const char* s1, const char* s2){ return ::strcmp(s1,s2)<0; }
};
int main(){
char** env = environ;
size_t sz = 0;
for(;*env; env++,sz++) {;} //measure the env
//?
qsort(environ, sz, sizeof(char*), (int (*)(const void*, const void*)) strcmp);
/*std::sort(environ, environ + sz, CstrLess{});*/
env = environ;
while(*env){
printf("%s%c", *env++, '\0');
}
return 0;
}
qsortpasschar**tostrcmp, notchar*.