I need to write a comparison function for qsort
for structs with pointer members. Let's say I have
struct foo2_struct
{
int a;
}
struct foo1_struct
{
int a;
struct foo2_struct *b;
}
Here's what I have:
int cmp_foo1(const void *a, const void *b)
{
struct foo1_struct foo1a = *((struct foo1_struct) a);
struct foo1_struct foo1b = *((struct foo1_struct) b);
int cmp = foo1a.a - foo1b.a;
if (cmp == 0)
{
if (foo1a.b == foo1b.b)
return 0;
//how to continue???
}
return cmp;
}
Note that foo2_struct
member b
is not unique, as in, two different variables of this type can have the same value of b
.