Solved

C qsort compare function issue

Posted on 2017-03-16
6
84 Views
Last Modified: 2017-03-16
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;

Open in new window


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;
}

Open in new window


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.
0
Comment
Question by:Dr. Klahn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 42052215
http://ideone.com/iizCtE
See if this works for you.
static int ib_cmpfunc (const void *a, const void *b) {
  const ib_arrayelement* aa = reinterpret_cast<const ib_arrayelement*>(a);
  const ib_arrayelement* bb = reinterpret_cast<const ib_arrayelement*>(b);
  if ( aa->ib_blackaddr - bb->ib_blackaddr > 0) return 1;
  if ( aa->ib_blackaddr - bb->ib_blackaddr < 0) return -1;
  return 0;
}

Open in new window

0
 
LVL 28

Author Comment

by:Dr. Klahn
ID: 42052219
Doesn't seem to be available on my system, at least not with the includes I'm using.  Perhaps it's only available in C++ ?

root@www: cc test.c
test.c: In function 'ib_cmpfunc':
test.c:22:31: error: 'reinterpret_cast' undeclared (first use in this function)
   const ib_arrayelement* aa = reinterpret_cast<const ib_arrayelement*>(a);
                               ^
test.c:22:31: note: each undeclared identifier is reported only once for each function it appears in
test.c:22:48: error: expected expression before 'const'
   const ib_arrayelement* aa = reinterpret_cast<const ib_arrayelement*>(a);
                                                ^
test.c:23:48: error: expected expression before 'const'
   const ib_arrayelement* bb = reinterpret_cast<const ib_arrayelement*>(b);

Open in new window

0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 42052230
Right, C, not C++.

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

Open in new window

0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 28

Author Comment

by:Dr. Klahn
ID: 42052233
Thanks very much, phoffric.  The second version of the compare function reversed the array, but there was no compile error.  Shortly after reviewing (again, but at least for the last time) some qsort compare example functions I ended up with this:

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

Open in new window


And it works as hoped-for, the masks are still "attached" to their CIDR blocks as the array is sorted.

mod_ipblock:  Blacklist dump
  Entry 0 38.38.38.0 mask 255.255.255.0
  Entry 1 38.38.0.0 mask 255.255.0.0
  Entry 2 38.0.0.0 mask 255.0.0.0
  Entry 3 255.255.255.255 mask 255.255.255.255
  Entry 4 87.87.232.0 mask 255.255.255.0
  Entry 5 16.255.0.0 mask 255.255.252.0
mod_ipblock:  Blacklist dump
  Entry 0 16.255.0.0 mask 255.255.252.0
  Entry 1 38.0.0.0 mask 255.0.0.0
  Entry 2 38.38.0.0 mask 255.255.0.0
  Entry 3 38.38.38.0 mask 255.255.255.0
  Entry 4 87.87.232.0 mask 255.255.255.0
  Entry 5 255.255.255.255 mask 255.255.255.255

Open in new window

0
 
LVL 28

Author Closing Comment

by:Dr. Klahn
ID: 42052234
Glad to have this one out of the way.  Excellent support from phoffric.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 42052241
Glad to have helped.
Keep in mind for future reference that the . operator has higher precedence that the (type) operator.
http://en.cppreference.com/w/c/language/operator_precedence
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question