There are at least two ways of fixing the sort code. One is to revise the comparator function to match the call to qsort(); the other is to fix the call to qsort() to match the comparator. The correct fix depends on the definition of the array - but the simplest declaration is an array of structures (not of structure pointers). Hence, this working code - which uses the original comparator but a different call to qsort():
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char *ip;
} mystruct;
static mystruct a_struct[] =
{
"66.249.71.3",
"190.148.164.245",
"207.46.232.182",
"190.148.164.245",
"190.148.164.245",
"202.154.114.253",
"190.148.164.245",
"190.148.164.245",
"66.249.71.3",
"190.148.164.245",
"202.154.114.253",
};
/* qsort */
static int struct_cmp(const void *a, const void *b)
{
mystruct *ia = (mystruct *)a;
mystruct *ib = (mystruct *)b;
return strcmp(ia->ip, ib->ip);
}
static void print_list(mystruct *list, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%2d: %s\n", i, list[i].ip);
}
#define DIM(x) (sizeof(x)/sizeof(*(x)))
int main(void)
{
print_list(a_struct, DIM(a_struct));
qsort(a_struct, DIM(a_struct), sizeof(mystruct), struct_cmp);
print_list(a_struct, DIM(a_struct));
}
This just leaves us with an alphanumerically sorted array of values, with all the '190.x.y.z' addresses appearing before the others, etc. Fixing that requires a more complex comparator - one solution for which is described by steveha in his answer.