Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//code below
typedef struct index
{ // 1 entry in index contains these 3 fields
char key[7];
int rrn;
int block;
} Index;
int compare(const void *p1, const void *p2);
int main(void)
{
int i;
Index the_index[3];
the_index[0].key[0] = 0;
the_index[1].key[0] = 0;
the_index[2].key[0] = 0;
strcat(the_index[0].key, "N87BC ");
strcat(the_index[1].key, "TC-THG");
strcat(the_index[2].key, "N1079U");
puts("Before sort:\n");
for (i = 0; i < 3; i++) printf("%s\t", the_index[i].key);
puts(" ");
qsort((void *) the_index, sizeof(the_index)/sizeof(the_index[0]), sizeof(Index), compare);
puts("After sort:\n");
for (i = 0; i < 3; i++) printf("%s\t", the_index[i].key);
puts(" ");
return(0);
}
int compare(const void *p1, const void *p2)
{
return strcmp(((Index*)p1)->key, ((Index*)p2)->key);
}