Search code examples
c++gcc-warning

How to properly cast char ** x to const char **


I have the following variable.

char **arr

Then I want to perform some modification on array which means it can't be declared as a constant.

Now I have a function that accepts the argument of type const char ** arr. But I have no control over the signature of this function.

Now when I cast arr to const char ** arr g++ generates a warning which is [-Werror=cast-qual].

For more clarification consider following MCVE:

#include<cstdio>

void print(const char** x){
    printf("%s", x[0]);
}

int main(int argc, char **argv){
    if(argc>1){
        print((const char **)argv);     
    }
     return 0;
}

//Then compile it as follow:

$ g++ -Wcast-qual test.cpp

//gives the following output:

MCVE.cpp: In function ‘int main(int, char**)’:
MCVE.cpp:5:36: warning: cast from type ‘char**’ to type ‘const char**’ casts away qualifiers [-Wcast-qual]
   const char ** q = (const char**) argv;


So my question is why this generates a warning? Is there any risk in doing this?

And how to achieve a behavior I want to achieve?


Solution

  • Allowing a cast from char** to const char** provides a loophole to modify a const char or a const char*.

    Sample code:

    const char c = 'A';
    
    void foo(const char** ptr)
    {
       *ptr = &c; // Perfectly legal.
    }
    
    int main()
    {
       char* ptr = nullptr;
       foo(&ptr);  // When the function returns, ptr points to c, which is a const object.
       *ptr = 'B'; // We have now modified c, which was meant to be a const object.
    }
    

    Hence, casting a char** to const char** is not a safe cast.


    You can use

    if(argc>1)
    {
       const char* ptr = argv[0];
       print(&ptr);     
    }
    

    for your code to compile without the cast-qual warning.

    If you need to pass more than just the first argument, you'll need to construct an array of const char* and use it.

    if(argc>1)
    {
       int N = <COMPUTE N FIRST>;
       const char** ptr = new const char*[N];
       for (int i = 0; i < N; ++i )
       {
          ptr[i] = argv[i];
       }
    
       print(ptr);
    
       delete [] ptr; // Make sure to deallocate dynamically allocated memory.
    }