I keep getting the error "Conversion from string literal to char* is deprecated" in my code. The purpose of the code is to use a pointer-to-pointer to assign string1 and string2 a word, then print it out. How can I fix this?
Here is my code:
#include <iostream>
using namespace std;
struct WORDBLOCK
{
char* string1;
char* string2;
};
void f3()
{
WORDBLOCK word;
word.string1 = "Test1";
word.string2 = "Test2";
char *test1 = word.string1;
char *test2 = word.string2;
char** teststrings;
teststrings = &test1;
*teststrings = test2;
cout << "The first string is: "
<< teststrings
<< " and your second string is: "
<< *teststrings
<< endl;
}
C++ string literals are arrays of const
char
, which means you can't legally modify them.If you want to safely assign a string literal to a pointer (which involves an implicit array-to-pointer conversion), you need to declare the target pointer as
const char*
, not just aschar*
.Here's a version of your code that compiles without warnings:
Consider what could happen if the language didn't impose this restriction:
A (non-
const
)char*
lets you modify the data that the pointer points to. If you could assign a string literal (implicitly converted to a pointer to the first character of the string) to a plainchar*
, you'd be able to use that pointer to modify the string literal with no warnings from the compiler. The invalid code above, if it worked, would print-- and it might actually do so on some systems. On my system, though, it dies with a segmentation fault because it attempts to write to read-only memory (not physical ROM, but memory that's been marked as read-only by the operating system).
(An aside: C's rules for string literals are different from C++'s rules. In C, a string literal is an array of
char
, not an array ofconst char
-- but attempting to modify it has undefined behavior. This means that in C you can legally writechar *s = "hello"; s[0] = 'H';
, and the compiler won't necessarily complain -- but the program is likely to die with a segmentation fault when you run it. This was done to maintain backward compatibility with C code written before theconst
keyword was introduced. C++ hadconst
from the very beginning, so this particular compromise wasn't necessary.)