Search code examples
c++gccmingw32string-literals

Disable warning "deprecated conversion from string constant to 'char*' [-Wwrite-strings]"


I have these two lines in my code:

RFM2G_STATUS   result;   
result = RFM2gOpen( "\\\\.\\rfm2g1", &rH );

I get the error message:

"warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
     result = RFM2gOpen( "\\\\.\\rfm2g1", &rH );"

Actually I can not modify it to

const RFM2G_STATUS   result;   

because RFM2G_STATUS is pre-defined in another file and does not accept const before. Is there another way to disable this warning message?


Solution

  • You seem to be absolutely sure that RFM2gOpen does not modify the input string, otherwise you would have undefined behavior in your code as it stands now.

    If you are sure that the input data will not be written to, you can const_cast the constness away safely:

    result = RFM2gOpen(const_cast<char*>("\\\\.\\rfm2g1"), &rH );
    

    Again, this is only safe if the routine does not write to the input string, ever, otherwise this is undefined behavior!

    If you are not completely sure that this method will never write to the character array, copy the string to an std::vector<char> and pass the .data() pointer to the function (or use a simple char array as Bo Persson suggests, that would most likely be more efficient/appropriate than the vector).