troubleshooting Question

C qsort compare function issue

Avatar of Dr. Klahn
Dr. Klahn asked on
ProgrammingC
6 Comments1 Solution526 ViewsLast Modified:
I'm attempting to sort what would normally be a multi-dimensional array (CIDR block in 0, CIDR mask in 1) as a single-dimensional array of structures.  Using qsort, telling it the data size is the size of the structure, and a hopefully-clever compare function to compare only the CIDR blocks using structure offsets.

The compilation fails in the compare function with the error:

sorttest.c: In function 'ib_cmpfunc':
sorttest.c:22:27: error: request for member 'ib_blackaddr' in something not a structure or union
   if ((*(ib_arrayelement)a.ib_blackaddr - *(ib_arrayelement)b.ib_blackaddr) > 0) return 1;
                           ^
sorttest.c:22:62: error: request for member 'ib_blackaddr' in something not a structure or union
   if ((*(ib_arrayelement)a.ib_blackaddr - *(ib_arrayelement)b.ib_blackaddr) > 0) return 1;
                                                              ^
sorttest.c:23:27: error: request for member 'ib_blackaddr' in something not a structure or union
   if ((*(ib_arrayelement)a.ib_blackaddr - *(ib_arrayelement)b.ib_blackaddr) < 0) return -1;
                           ^
sorttest.c:23:62: error: request for member 'ib_blackaddr' in something not a structure or union
   if ((*(ib_arrayelement)a.ib_blackaddr - *(ib_arrayelement)b.ib_blackaddr) < 0) return -1;

Here's the code used to generate the test program.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

#define BLACKLIST_SIZE 512

typedef struct {
    /* IPV4 address as 32-bit integer */
  unsigned long ib_blackaddr;
    /* CIDR mask as 32-bit integer */
  unsigned long ib_blackmask;
}  ib_arrayelement;

  /* Number of entries in the blacklist table */
int ib_blacklist_size;

  /* IPv4 address - CIDR mask blacklist */
ib_arrayelement ib_blackarray[BLACKLIST_SIZE];

static int ib_cmpfunc (const void *a, const void *b) {
  if ((*(ib_arrayelement)a.ib_blackaddr - *(ib_arrayelement)b.ib_blackaddr) > 0) return 1;
  if ((*(ib_arrayelement)a.ib_blackaddr - *(ib_arrayelement)b.ib_blackaddr) < 0) return -1;
  return 0;
}

void main (int argc, char **argv) {

  qsort(ib_blackarray, ib_blacklist_size,
        sizeof(ib_arrayelement), ib_cmpfunc);

  return;
}

The qsort compare function is too deep in pointers for me to follow what the problem is; I don't normally do anything this obscure.  Can someone point me at a solution?

Side note:  This is c on a Debian linux host.  Not C++, nor Windows.
ASKER CERTIFIED SOLUTION
phoffricSoftware Engineering and Matlab Analyst

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros