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;
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.
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.ctest.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 intest.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);
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
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;}
See if this works for you.
Open in new window