I am trying to get the binary search library function to work for a dynamically created structure array. It crashes with the dreaded "Windows encountered a problem and cannot continue" message. Can anyone tell me what I have done wrong? Here is my code:#include <stdio.h>
#include <stdlib.h>
typedef struct name {
char first[10];
char middle[10];
} Name;
Name **names;
char *firsts[5]={"alicia","anna","tim","tom","tonya"};
char *middles[5]={"louise","anne","joseph","bayne","marie"};
int i;
Name *ptr;
main(int argc, char *argv[]) {
int compare(Name *name1, Name *name2);
names=malloc(5*sizeof(char *));
for (i=0;i<5;i++) {
names[i]=malloc(sizeof(Name));
strcpy(names[i]->first,firsts[i]);
strcpy(names[i]->middle,middles[i]);
}
ptr=bsearch("tom",names,5,sizeof(Name),compare);
if (ptr!=NULL) printf("ptr->middle=%s|", ptr->middle);
else printf("\nnot found\n");
}
compare(Name *name1, Name *name2) {
return strcmp(* (char**) name1->first, * (char **) name2->first);
}