double-pointer

How to qsort an array of pointers to char in C?

a 夏天 提交于 2019-11-26 05:33:51
问题 Suppose I have an array of pointers to char in C: char *data[5] = { \"boda\", \"cydo\", \"washington\", \"dc\", \"obama\" }; And I wish to sort this array using qsort: qsort(data, 5, sizeof(char *), compare_function); I am unable to come up with the compare function. For some reason this doesn\'t work: int compare_function(const void *name1, const void *name2) { const char *name1_ = (const char *)name1; const char *name2_ = (const char *)name2; return strcmp(name1_, name2_); } I did a lot of