I have an array
char* m[] = {"AA", "AC", "AG", "AT", "CA", "CC", "CG", "CT", "GA", "GC", "GG", "GT", "TA", "TC", "TG", "TT"};
and Im trying to get the index of the entry that matches pseq = (char*)malloc(2);
I am just using a basic compare function right now, as I hope to create a specific one later
int compare(const void *a, const void *b)
{
return strcmp(a, b);
}
size_t mlen = sizeof(m)/sizeof(m[0]);
My attempt at bsearch is:
char* q = bsearch(&pseq, m, mlen, sizeof(m[0]), compare);
However, every return is NULL.
But compare(pseq, m[5]) = 0
for pseq = "CC"
The pointers passed to compare
are pointers to char*
, so you have to dereference them to get char*
that point at strings to be compared.
int compare(const void *a, const void *b)
{
return strcmp(*(char**)a, *(char**)b);
}
Also
pseq = (char*)malloc(2);
looks bad because
malloc()
in C is discouraged.