So im trying to do a binary search in an array of strings called conj_str the thing is to do that i have to sort it and to that im trying to use qsort the problem is that comparsion function isnt working and its not sorting anything.
Program:
#include<stdlib.h>
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024
#define MAX_SIZE 10000
int compare (const void *a, const void *b)
{
const char *key = a;
const char * const *arg = b;
return strcmp(key, *arg);
}
int main()
{
int i;
char conj_str[MAX_SIZE][MAX_CHARS];
size_t len = sizeof(conj_str)/sizeof(const char *);
strcpy(conj_str[0],"fcb");
strcpy(conj_str[1],"bvb");
strcpy(conj_str[2],"slb");
strcpy(conj_str[3],"fcp");
strcpy(conj_str[4],"rma");
qsort (conj_str, len, sizeof (const char *), compare);
for (i = 0; i < 5; i++) {
printf ("%d: %s\n", i, conj_str[i]);
}
}
In this call
there is specified incorrectly the size of array element. There must be
Also this statement
does not make sense.
And in the comparison function this declaration
also does not make sense.
The function can look like
and substitute this statement
for
Here is your program with minor changes.
Its output is