I am having trouble getting the following program to run. I know I am doing something wrong but can't figure out what it is. It compiles and runs the qsort but crashes when it tries to do the bsearch. Can someone help? Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char source[129]="";
char *dta[5]={"muid","epicsid","ssn","inst","chkdat"};
char *rec[200];
char input[256];
char key[10];
char *ans;
int i,e;
int main(int argc, char *argv[]) {
int strcmp();
int compare(const void *arg1, const void *arg2);
for (i=0;i<5;i++) {
strcpy(input,dta[i]);
rec[i]=malloc(strlen(input)+1);
strcpy(rec[i],input);
}
qsort((void *)rec,(size_t)i,sizeof(char *),compare);
e=i;
for (i=0;i<e;i++) printf("%s\n",rec[i]);
strcpy(key,"inst");
ans=bsearch(key,(void *)rec,(size_t)e,sizeof(char *),compare);
if (ans!=NULL) printf("\nfound inst");
strcpy(key,"inst1");
ans=bsearch(key,(void *)rec,(size_t)e,sizeof(char *),compare);
if (ans==NULL) printf("\ndid not find inst1");
}
int compare(const void *arg1, const void *arg2) {
return strcmp(* (char**) arg1, * (char**) arg2);
}