Ok I'm getting a little tired of the running in circles (part of which is my fault). Here is a sample code I wrote to do eactly what want. Only difference is that you'll have to use strcmp() instead of a stright equality check
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int value;
}Bob;
int myrandom(int max) {
return (int)( ((float)rand() / RAND_MAX ) * max );
}
int findValue(Bob *[], const int, const int);
int main()
{
int i,j, index;
Bob *array[10], *temp;
for (i=0; i < 10; i++)
array[i] = malloc(sizeof(Bob));
for (i=0; i < 9; i++)
array[i]->value = myrandom(20);
array[9]->value = 12;
for (i=0; i < 9; i++)
for (j=i+1; j<10; j++)
if ( array[i]->value > array[j]->value )
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
index = findValue(array, 10, 12);
if ( index == -1 )
puts("Could not find that value");
else
printf("Found the value at index %d which has the value of %d\n", index, array[index]->value);
for (i=0; i<10; i++)
free(array[i]);
return 0;
}
int findValue(Bob *array[], const int size, const int value) {
Bob **low = array;
Bob **high = array + size -1;
Bob **mid = ((low-array)+(high-array)) / 2 + array;
while ( high > low )
{
if ( (*mid)->value == value )
return mid - array;
if ( (*mid)->value > value )
{
high = mid - 1;
mid = ((low-array)+(high-array)) / 2 + array;
}
if ( (*mid)->value < value )
{
low = mid + 1;
mid = ((low-array)+(high-array)) / 2 + array;
}
}
return -1;
}
I hope this clears up any confusion. Sorry for being so dense before. Sometimes I miss the most obvious things