Search code examples
c++type-conversionconstantsextern-c

Can I have a pointer on pointer on const in C++?


Basically I am wondering whether something like

f(const void* a){
  // This is also what `(char**) a` would do behind the scenes
  char** string_a_ptr {reinterpret_cast<char**>(const_cast<void*>(a))}; 

  // ...

is possible while preserving the const qualifier.

My question came up during an exercise about C-Arrays in C++. Because of C, I had to pass a function pointer with const void* arguments. In this case it was pointing on an array of C-strings. To cast it to the correct type I had to cast the const away. Is there a way in C++ to get something like a "(const char*)*"` pointer?

I have tried out const char** (without the const_cast) which yields

reinterpret_cast from type ‘const void*’ to type ‘const char**’ casts away qualifiers

and (const*)*, which gives a syntax error

expected type-specifier before ‘(’ token

This is a purely academic question. I am aware, that the problem can be solved easier and saver using std::string and #include <algorithm>.

While I am at it, are there advantages of sometimes using C-constructions in C++ or should they always be avoided if possible? I only did some small scale exercise problems with C code in C++, but it already causes quite a few headaches and forces me to look really closely under the hood, even though I have written a fair amount of C code in my life. Or is it would you rather recommend using the extern C qualifier and writing C code in pure C?


Solution

  • That would be a char* const *.

    There are three places in a double pointer to put const:

    const char**
    

    Pointer, to a pointer, to a character you cannot change.

    char *const *
    

    pointer, to a pointer you cannot change, to a character.

    char ** const
    

    pointer you cannot change, to a pointer, to a character.

    You can mix these to make different parts const.