I have an array of structs and I want to B-Search these by a value inside of them. I am having trouble because I need to return the index (not address) after searching through the array of structs (typedef structs). This is what I have, and I am not sure my logic behind it is even right. Can somebody please help.
Code:
int findValue(StructType * const array[], int size, const String target) {
StructType * leftPtr = * array;
StructType * rightPtr = (* array) + size - 1; // error for the + sign
StructType * midPtr;
while (strcmp((*leftPtr).value, (*rightPtr).value) <= 0) {
midPtr = (leftPtr + rightPtr) / 2;
while(strcmp((*midPtr).value, target) < 0) {
leftPtr = midPtr + 1;
midPtr = (leftPtr + rightPtr) / 2;
}
if (strcmp((*midPtr).value, target) == 0)
return midPtr - array;
}
return -1;
}