C++ Warning: Deprecated Conversion from String Constant to 'Char*' [-Wwrite-Strings]

How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC?

I believe passing -Wno-write-strings to gcc will suppress this warning.

C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

String literals are an array of const char, we can see this from the draft C++ standard section 2.14.5 String literals which says (emphasis mine):

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

so this change will remove the warning:

const char * commandsForGnuplot[] = {"set title \"Probability Graph\"", "plot     'data.temp' with lines"};
^^^^^

Note, allowing a *non-const char** to point to const data is a bad idea since modifying a const or a string literal is undefined behavior. We can see this by going to section 7.1.6.1 The cv-qualifiers which says:

Except that any class member declared mutable (7.1.1) can be modified,
any attempt to modify a const object during its lifetime (3.8) results
in undefined behavior.

and section 2.14.5 String literals which says:

Whether all string literals are distinct (that is, are stored in
nonoverlapping objects) is implementation defined. The effect of
attempting to modify a string literal is undefined.

getting error as deprecated conversion from string constant to 'char*' [-Wwrite-strings]

"" is a c-style string literal whose type is const char[1] and could convert to const char* implicitly. Implicit conversion to char* is not allowed since C++11.

In C, string literals are of type char[], and can be assigned directly to a (non-const) char*. C++03 allowed it as well (but deprecated it, as literals are const in C++). C++11 no longer allows such assignments without a cast.

Making initgraph taking const char* is the best. Otherwise you have to perform explicit conversion with const_cast; but note that attempting to modify a string literal results in undefined behavior. If initgraph would do, then you shouldn't pass a string literal.

How deal with the warning deprecated conversion from string constant to char*?

declare the first argument of printmatrix as a std::string or a pointer to char const.

by the way, when the problem is with a function called printmatrix, why not include that in the sample code.


background info: the type of the string literal "blah" is an array of 5 char const values (the fifth is the terminating zero). in C narrow string literals can be implicitly converted to char*, and this was supported in the first C++ standard, C++98, but was deprecated already then. the conversion was removed in C++11.

warning: deprecated conversion from string constant to 'char*''

You don't lose any re-pointing options by making the array a const char* arr[]. Note that there's a huge difference between const char * p (a mutable pointer to an immutable char) and a char * const p (an immutable pointer to a mutable char). This code is perfectly valid:

const char *arr[] = {
"xyz",
"abc",
"pqr",
NULL
};

arr[1] = "ghi";

Live example

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

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).

warning: deprecated conversion from string constant to ‘char*’ when i add a formal parameter in the function def but with no actual parameter

Adding const solves your problem:

void GetHMACCode(unsigned char* buffer,
long bufferLength,
unsigned char** pResult,
unsigned int &nResultLen,
const char *extra_arg = "11111111111111111111") // const added here
{}


Related Topics



Leave a reply



Submit