Question

Hello I have some problems with the bsearch() function. I get the "Access violation reading location" exception. I'm using it as follows:

typedef char **arstr;

int compareexp(const void *a, const void *b){ 
    return strcmp(*(const arstr)a, *(const arstr)b);
}

binsrch = bsearch(&key, file_array, linecount, WORDMAX+2, compareexp);

'file_array' is my dynamic string array with 2401 elements. each element is max 22 chars long imported from a *.txt document.

'linecount' is the integer 2402 and refers to the line number from the same *.txt document.

'WORDMAX' is defined as 20

I don't really get the reason why my function is failing. I guess there is something wrong with the values from linecount or WORDMAX.

Was it helpful?

Solution

After looking at the example from the like it seems like you either might be missing a * in the comperison or WORDMAX +2 not is the right approach. Try changing that WORDMAX to get the size of the struct you are using.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top