I have a list of words that are separated by empty spaces given by the user input. I am using qsort to sort them and qsort requires the sorting function to use a void type. While I am trying to change the void type to string and print the results, I get weird characters printed.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
int compareStrings(const void *x_void, const void *y_void){
// strings are compared here in lexicographical order
char *word1 = (char *) &x_void;
char *word2 = (char *) &y_void;
printf("%s \n", word1);
printf("%s \n", word2);
return 0;
}
int main(){
int size;
// inputs are taken here
scanf("%d *[^\n]", &size);
char *sentence = malloc(size 1);
fgets(sentence, size 1, stdin);
int spaceAmount = 0;
// In this loop the amount of space characters are determined.
findSpaces(sentence);
// The amount of spaces are used to allocate memory for this list of words.
char **allWords = malloc((spaceAmount 1) * sizeof(char*));
int seperator = 0;
int index = 0;
// allWords list is populated here in this loop with words from the
// sentence variable.
populateAllWords(allWords);
free(sentence);
qsort(allWords, spaceAmount 1, sizeof(char*), compareStrings);
for (int i = 0; i < spaceAmount 1; i ){
printf("%s \n", allWords[i]);
}
free(allWords);
return 0;
}
here I give input from the console, the size of sentence and the sentence it self. What the code should normally do here is that it should print the words one by one but I am gettin weird results. Here are the console logs:
$ ./result.exe
1000
hello I am Tim
─↔y
└↔y
╚↔y
─↔y
╠↔y
╚↔y
─↔y
└↔y
╚↔y
─↔y
─↔y
└↔y
what am I doing wrong? Please help. I added a minimal main function also.
CodePudding user response:
The way that callback functions from bsearch/qsort works is that they give you a pointer to an item. With qsort(allWords,...
you essentially pass an array of pointers. Every item is a pointer, so any pointer passed from qsort
to your callback will be a pointer to pointer.
void*
is tricky here since it's the generic object pointer type and you can cast to/from it even when you have a pointer to pointer. (Please note that void**
is however not such a generic pointer type.)
Therefore your callback should be written as:
int compareStrings (const void* obj1, const void* obj2){
// strings are compared here in lexicographical order
const char *word1 = *(const char**)obj1;
const char *word2 = *(const char**)obj2;
printf("%s \n", word1);
printf("%s \n", word2);
return strcmp(word1, word2); // something meaningful not just 0
}