-4

As I know in C++ you can change the value of (char *) variable , but I have an error.

This is a screenshot.

The error is:

error C2440: '=': cannot convert from 'const char [71]' to 'char *'

before I declared a char* variable:

char *sql;

and then I want to use it, but:

sql = "INSERT INTO person (id,name,last,age) "  \
    "VALUES (1, 'Paul', 'Yezh', 14); ";

On the equal sign I have this error.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • 1
    Possible duplicate of [cannot convert from 'const char \[3\]' to 'char \*' x100000 (Qt Creator C++ Windows 32)](https://stackoverflow.com/questions/34244225/cannot-convert-from-const-char-3-to-char-x100000-qt-creator-c-windows) – 1201ProgramAlarm Jul 11 '18 at 23:49
  • Mostly duplicate: [How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC?](https://stackoverflow.com/questions/59670/how-to-get-rid-of-deprecated-conversion-from-string-constant-to-char-warnin) The warning has since been promoted to an error because, face it, assigning `const` data of any stripe to a non-const` pointer is a bad idea. By the way, ignore the accepted answer. It's no longer valid. – user4581301 Jul 11 '18 at 23:55
  • 1
    [Please don't post images of code](https://meta.stackoverflow.com/a/285557/2745495). Instead, add and **[format](https://stackoverflow.com/help/formatting)** your code into the post itself as **text**. – Gino Mempin Jul 12 '18 at 00:03

1 Answers1

3

Since character strings are constant arrays, you need to change the type of sql to const char *sql.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • Or copy the string if it needs to be modified. – eesiraed Jul 11 '18 at 23:45
  • This answer is correct. Consider also that all compiler errors are well documented online. See [here](https://msdn.microsoft.com/en-us/library/sy5tsf8z.aspx) for a clear and easy explanation of the C2440 error. Always check documentation. – brianolive Jul 11 '18 at 23:57