How to Get Rid of 'Deprecated Conversion from String Constant to 'Char*'' Warnings in Gcc

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.

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.

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
{}

Deprecated conversion from string constant to char * error

If a function takes a char const *, it guarantees that it only reads whatever data the pointer points to. However, if it takes a non-const pointer, like char *, it might write to it.

As it is not legal to write to a string literal, the compiler will issue a warning.

The best solution is to change the function to accept char const * rather than char *.

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.

deprecated conversion from string constant to 'char*' in c

This should be a warning (though you may have set your compiler to treat warnings as errors, which is often a good idea).

What you want is: char const *p = "hello"; instead.

Attempting to modify a string literal gives undefined behavior. The const prevents you from doing that by accident (i.e., code that attempts to write via the pointer won't compile, unless you remove the const qualifier, such as with a cast).

C++ deprecated conversion from string constant to 'char*'

This is an error message you see whenever you have a situation like the following:

char* pointer_to_nonconst = "string literal";

Why? Well, C and C++ differ in the type of the string literal. In C the type is array of char and in C++ it is constant array of char. In any case, you are not allowed to change the characters of the string literal, so the const in C++ is not really a restriction but more of a type safety thing. A conversion from const char* to char* is generally not possible without an explicit cast for safety reasons. But for backwards compatibility with C the language C++ still allows assigning a string literal to a char* and gives you a warning about this conversion being deprecated.

So, somewhere you are missing one or more consts in your program for const correctness. But the code you showed to us is not the problem as it does not do this kind of deprecated conversion. The warning must have come from some other place.



Related Topics



Leave a reply



Submit