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 const
s 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.
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 areconst
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.
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
{}
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
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.
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.
Related Topics
Optimizing Away a "While(1);" in C++0X
Embedding Resources in Executable Using Gcc
Multi-Character Constant Warnings
What Does 'Unsigned Temp:3' in a Struct or Union Mean
What Is the Purpose of the Most Vexing Parse
Why How to Not Push_Back a Unique_Ptr into a Vector
Why Can't the Template Argument Be Deduced When It Is Used as Template Parameter to Another Template
Function Parameter Evaluation Order
Which Is the Fastest Algorithm to Find Prime Numbers
Difference Between an Int and a Long in C++
Operator Precedence VS Order of Evaluation
Why Do C and C++ Compilers Allow Array Lengths in Function Signatures When They'Re Never Enforced