+ 1

how do you sort rows of string on an array using qsort?

The 'char str_array[n][len]', not the 'char *str_array[]' format.

12th Jan 2020, 12:52 PM
CASOY
CASOY - avatar
5 Respuestas
+ 3
CASOY Hahaha yeah, I was kinda doubt it was anyways, no problem 👌 Then you mean to sort multiple rows of string? Like this? Andy Bernard Chloe ... if so, I guess you need to edit the question to add more details 👍 (Edit) For sorting rows of C string we can use `strcmp` in comparison function that is used by `qsort`. https://code.sololearn.com/c2D6GFUg1VRg/?ref=app
12th Jan 2020, 3:07 PM
Ipang
+ 2
This example uses static C String array. I'm not sure (haven't tested) using dynamic array yet. https://code.sololearn.com/ccxP61geugHi/?ref=app
12th Jan 2020, 1:38 PM
Ipang
+ 1
Thankyou for that Ipang however it's kind of not what I am looking for, Iwant only the string elements to be sorted not the character on each string. I appreciate the help. 😁
12th Jan 2020, 2:44 PM
CASOY
CASOY - avatar
+ 1
Ipang sorry about that. 😅 Thats a realy great help! thankyou so much.
12th Jan 2020, 10:38 PM
CASOY
CASOY - avatar
+ 1
//You define an 2 dimensional array of strings using #defines to set the dimensions char myWords[MAX_WORD][MAX_WORD_LEN + 1]; //Keep track of how many word you add with an int int word_count = 0; // make sure to increase whenever you add a word!!! I did not put // this bit in present sample //You define a wrapper function that accepts void pointers and then itself //calls strcmp to avoid GCC warning flags inline int strcmp_void_wrapper(const void *p1, const void *p2) { return strcmp(p1, p2); } //You call qsort using your char 2D array, the actual word count, the max size of // one word and your wrapper function. qsort(myWords, word_count, MAX_WORD_LEN + 1, strcmp_void_wrapper); I do not know how to make qsort collaborate with dynamic string array (char *[] instead of char[][]) because the size of each word could be different. If someone has an answer to that, I would greatly appreciate.
20th Sep 2022, 8:45 PM
ltmf