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

I have a class with private char str[256];

and for him I have an explicit constructor:

 explicit myClass(const char *func) { strcpy(str,func); } 

I call it like:

 myClass obj("example"); 

When I compile this, I get the following warning:

deprecated conversion from string constant to 'char *'

Why is this happening?

+139
c ++ string explicit-constructor
Oct 06 '09 at 8:45
source share
11 answers

This is the error message that you see whenever you encounter this situation:

 char* pointer_to_nonconst = "string literal"; 

What for? Well, C and C ++ are different in type of string literal. In C, the type is a char array, while in C ++ it is a constant char array. In any case, you are not allowed to change the characters of a string literal, so const in C ++ is actually not a limitation, but rather a type safety issue. Converting from const char* to char* usually not possible without an explicit cast for security reasons. But for backward compatibility with C, the C ++ language still allows you to assign the string literal char* and gives a warning that this conversion is not recommended.

So, somewhere you are missing one or more const in your program for const to be correct. But the code you showed us is not a problem, since it does not do this kind of obsolete conversion. The warning must have come from another place.

+128
Oct 06 '09 at 9:23
source share

Warning:

deprecated conversion from string constant to 'char *'

given because you are doing something (not in the code you posted), for example:

 void foo(char* str); foo("hello"); 

The problem is that you are trying to convert a string literal (with type const char[] ) to char* .

You can convert const char[] to const char* because the array decays to a pointer, but what you do makes the variable constant.

This conversion is probably allowed for C compatibility and just gives you the warning you mentioned.

+143
07 Oct. '09 at 13:32
source share

How to answer no. 2 by fnieto - Fernando Nieto clearly and correctly describes that this warning is given because somewhere in the code that you are doing (not in the code that you sent), something like:

 void foo(char* str); foo("hello"); 

However, if you want to save the code without warning, then just make the appropriate changes to your code:

 void foo(char* str); foo((char *)"hello"); 

That is, just add the string constant to (char *) .

+88
Dec 21 '10 at 17:18
source share

There are 3 solutions:

Solution 1:

 const char *x = "foo bar"; 

Solution 2:

 char *x = (char *)"foo bar"; 

Solution 3:

 char* x = (char*) malloc(strlen("foo bar")+1); // +1 for the terminator strcpy(x,"foo bar"); 

Arrays can also be used instead of pointers, because the array is already a constant pointer.

+33
Jul 10 '14 at 1:04
source share

In fact, the string constant literal is neither the constant char * nor char *, but char []. This is rather strange, but written in the C ++ specifications; If you change it, the behavior is undefined because the compiler can save it in a code segment.

+3
06 Oct '09 at 8:59
source share

I solve this problem by adding this macro at the beginning of the code somewhere. Or add it to <iostream> , hehe.

  #define C_TEXT( text ) ((char*)std::string( text ).c_str()) 
+2
Sep 15 2018-11-11T00:
source share

Maybe you can try this:

 void foo(const char* str) { // Do something } foo("Hello") 

It works for me

+2
Nov 20 '18 at 6:37
source share

I have the same problem too. And what I just did is just add const char * instead of char *. And the problem is solved. As mentioned above, this is a compatible bug. C treats strings as char arrays, while C ++ treats them as const char arrays.

0
12 Oct '15 at 15:19
source share

I find this simple wrapper class useful for converting C ++ strings to char * :

 class StringWrapper { std::vector<char> vec; public: StringWrapper(const std::string &str) : vec(str.begin(), str.end()) { } char *getChars() { return &vec[0]; } }; 
0
Feb 26 '18 at 13:30
source share

The worst part of this is the typical ambiguity in misusing the reserved word "string", which makes it possible to mistakenly believe that the "example" .c_str () will solve the problem.

0
Dec 13 '18 at 16:56
source share

The following solution is shown, your string is assigned to a variable pointer to a constant array from char (the string is a constant pointer to a char constant array - plus the length of the information):

 #include <iostream> void Swap(const char * & left, const char * & right) { const char *const temp = left; left = right; right = temp; } int main() { const char * x = "Hello"; // These works because you are making a variable const char * y = "World"; // pointer to a constant string std::cout << "x = " << x << ", y = " << y << '\n'; Swap(x, y); std::cout << "x = " << x << ", y = " << y << '\n'; } 
-one
Sep 16 2018-11-11T00: 00Z
source share



All Articles