>>/* array of entries */
>>struct ot_entry *optbl[OT_SIZE];
This is not an array of structs, its an array of pointers to structs.
I assume you're actually assigning a value to these pointers? If not they'll most likely all be null (I'm assuming this array is a global variable?
The sortOpTbl should be like so:
void sortOpTbl(struct ot_entry optbl[])
The call to qsort is also incorrect.
Here is a sample, you code amended to run:
(I change the comparison to compare the ints rather than the char arrays, just to save time).
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define OP_LENGTH 10
#define OT_SIZE 10
/* an entry in Operation Table */
struct ot_entry
{
char ot_mnemonic[OP_LENGTH + 1];
int ot_format;
};
/* array of entries */
struct ot_entry optbl[OT_SIZE];
int compare_elements(const void *ptr1, const void *ptr2)
{
return(((struct ot_entry *) ptr1)->ot_format - ((struct ot_entry *) ptr2)->ot_format);
}
// sort Operation Table
void sortOpTbl(struct ot_entry optbl[])
{
qsort(optbl, OT_SIZE, sizeof(struct ot_entry), compare_elements);
}
int main(void)
{
int i;
for (i = 0; i < OT_SIZE; i++)
{
optbl[i].ot_format = 10 - i;
printf ("%d ", optbl[i].ot_format);
}
sortOpTbl(optbl);
printf ("\nAfter:\n");
for (i = 0; i < OT_SIZE; i++)
{
printf ("%d ", optbl[i].ot_format);
}
return(0);
}