I need to search an array of structs, looking for a match of one of the struct items.
The struct is:
struct user_info {
char user [30];
char password[30];
char networkAddr[20];
char day[03];
char systime[15];
};
struct user_info *cache[1000];
I need to search for cache[]->user equals some username that is passed. The array can have anywhere from 0-1000 entries initially, possibly more using realloc when needed.
I'm trying to write a search based on one of my c books, but it's not working. I think it has trouble because in my testing I have only one entry in the array. I'm keeping a counter, user_count, so I always no how many entries should be in it. Here's what I have, can anyone help? I'm getting weird results.
searchCache(char user[30]) //binary search
{
int low, high, mid;
low = 0;
high = user_count - 1;
while (low <= high)
{
mid = (low + high)/2;
if (user < cache[mid]->user)
high = mid - 1;
else if (user > cache[mid]->user)
low = mid + 1;
else
return mid; /* found */
}
return -1;
}
Thanks,
James
The struct is:
struct user_info {
char user [30];
char password[30];
char networkAddr[20];
char day[03];
char systime[15];
};
struct user_info *cache[1000];
I need to search for cache[]->user equals some username that is passed. The array can have anywhere from 0-1000 entries initially, possibly more using realloc when needed.
I'm trying to write a search based on one of my c books, but it's not working. I think it has trouble because in my testing I have only one entry in the array. I'm keeping a counter, user_count, so I always no how many entries should be in it. Here's what I have, can anyone help? I'm getting weird results.
searchCache(char user[30]) //binary search
{
int low, high, mid;
low = 0;
high = user_count - 1;
while (low <= high)
{
mid = (low + high)/2;
if (user < cache[mid]->user)
high = mid - 1;
else if (user > cache[mid]->user)
low = mid + 1;
else
return mid; /* found */
}
return -1;
}
Thanks,
James