. Advertisement .
..3..
. Advertisement .
..4..
I am working on cpp, but I found the following warning message:
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
Is there any way to stabilize the issue “iso c++ forbids converting a string constant to ‘char*’”?
I read a lot of topics about this, but all of them were trying to install anything. Is this the correct way, or any recommendation for me?
Please find the beginning command below:
char const *q = "pin";
char const *r = "\n\r";
{
while(client.findUntil(*q, *r))
while(client.findUntil("pin", "\n\r"))
The cause: The warning shown that your program is ill-formed. You didn’t provide the declaration, but we can assume from the context that the
findUntil
argument ischar*
. A string literal cannot be passed to such a function.The solution: You can fix this error by copying string literals into editable arrays:
Another option is to fix
findUntil
to accept a pointer to const char instead. Then, because string literals can be transformed to a pointer to const char, you can utilize them.This warning indicates that your program is not well-formed. Although you didn’t display the declaration, context can help us to determine that
findUntil
‘s argument ischar*
. A string literal may not be passed to such a function.It was once well-formed, but now deprecated. To pass a string literal such as
char*
before C++11, it used to work.These statements are true by themselves.
This is absurd. You were trying to pass a string previously, but you now indirect through the character pointeder so that you are passing a character. This cannot work unless the function is a template.
findUntil(q, r)
will not work, either because pointers to const won’t implicitly transform to non-const.The best solution is to convert string literals into modifiable ranges.
Another option is to change
findUntil
so that it accepts a pointer-to const char instead. String literals can then be used, as they can be converted into a pointer-to const char.