Simplifying casting in my C/C++ qsort char** comparator function I have a comparator function I use with qsort to sort an array of char* (i.e., char**), which compiles with current versions of Clang and GCC without giving errors or warnings:

static int
comparator(const void* a, const void* b)
{
#ifdef __cplusplus
    auto a_recast = const_cast&lt;void *&gt;(a);
    auto b_recast = const_cast&lt;void *&gt;(b);
    auto a_rerecast = reinterpret_cast&lt;char **&gt;(a_recast);
    auto b_rerecast = reinterpret_cast&lt;char **&gt;(b_recast);
    auto a_rererecast = const_cast&lt;const char **&gt;(a_rerecast);
    auto b_rererecast = const_cast&lt;const char **&gt;(b_rerecast);
    return std::strcmp(*a_rererecast, *b_rererecast);
#else
    return strcmp(*(const char**) a, *(const char**) b);
#endif
}


It compiles conditionally, that is, depending on whether a C or C++ compiler is used, one or the other block of code is compiled. 

I offer both blocks, because this is part of a program that can be compiled alone in C, or in a larger suite of tools compiled with a C++ toolkit.

My question is about the work I had to do to in the C++ block, where I strip const, recast to char **, and reapply const.

Is there a simpler or cleaner way to do this C++-style casting, which generates the correct end results and does not result in warnings about C-style casting?

Clang, in particular, is chatty with warnings about old C-style casts, and I like my build logs to be clean, where fixes are realistic. 

I also like the code to be clear and easy to and maintain. I'd also like to inline this code, which would probably be easier for the compiler if the comparator function is smaller.

If I'm doing the correct work, that's fine, but if there are better ways, I'd be interested to learn them. Thanks for your advice.
How about this? 

int comparator(const void* a, const void* b)
{
#ifdef __cplusplus
    auto a_recast = static_cast&lt;const char* const *&gt;(a);
    auto b_recast = static_cast&lt;const char* const *&gt;(b);
    return std::strcmp(*a_recast, *b_recast);
#else
    return strcmp(*(const char* const *) a, *(const char* const *) b);
#endif
}


To elaborate, the reason that you had to go through the convoluted series of dangerous casts is that you actually were stripping constness. 
const void* means a promise not to change whatever the void turns out to be. That is, the thing pointed at by the rightmost star is const. const char ** on the other hand means a promise not to change the individual characters, but doesn't guarantee that you won't change the direct pointers to those characters. However, it's the pointers to those characters that are pointed at by the rightmost star. Intuitively perhaps, think of char * as being in a unit by itself a c_string, and your a and b are arrays of constant c_strings. 

The reason that clang warns about c style casts is precisely that they let you apply such a crazy list of operations and changing constnesses without necessarily realizing what is happening. 

As an aside, as far as the compiler is concerned, it is actually only necessary to enforce constness on the pointer. That is, you could also compile with static_cast&lt;char * const *&gt;. I put the additional const in because when your conceptual c_string is constant, you conceptually don't want to be changing any of the characters. (And you probably want the compiler to alert you if anything tries to do that.)