Arduino c++ compiler complains: warning: ISO C++ forbids converting a string...

I have among others this line of code:

char *host[NO_URL] = {"streaming.shoutcast.com", "swr-swr1-bw.cast.addradio.de",
                      "/swr/swr1/bw/mp3/64/stream.mp3", "airspectrum.cdnstream1.com",
                      "skonto.ls.lv", "icecast.omroep.nl", "beatles.purestream.net"
                      };

It is here that the compiler complains: "warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]"
I don't get it:

  • I'm not "converting" anything;
  • I've declared an array of pointers each pointing to a constant string, which I initialized.
    How do I silence this warning?

Put const before the char*.

wildbill:
Put const before the char*.

Better yet:

const char * const host[] = {"streaming.shoutcast.com", "swr-swr1-bw.cast.addradio.de",
                      "/swr/swr1/bw/mp3/64/stream.mp3", "airspectrum.cdnstream1.com",
                      "skonto.ls.lv", "icecast.omroep.nl", "beatles.purestream.net"
                      };

Thank you. I'll do that.
But, I still don't get it.
Declaration and initialization like this:
char *var = "Some string";
Makes var pointing to a constant string; so, what's the point in making it a constant again by doing:
const char *const var = "Some string";?

In the good old days before C++, it would be quite permissible in C to write on your string:

var[4]='X';

C++ doesn't like that (although I think you can still do it) so it issues warnings. Using const formalizes your agreement that you won't do any such thing.

wildbill:
In the good old days before C++, it would be quite permissible in C to write on your string:

var[4]='X';

C++ doesn’t like that (although I think you can still do it) so it issues warnings. Using const formalizes your agreement that you won’t do any such thing.

This is incorrect. Writing to string literals is not allowed in C either: String literals - cppreference.com

String literals are not modifiable (and in fact may be placed in read-only memory such as .rodata). If a program attempts to modify the static array formed by a string literal, the behavior is undefined.

Pieter

pagemaker:
so, what's the point in making it a constant again by doing:
const char *const var = "Some string";?

const char *const var = "Some string";

The red const is your promise to the compiler that you won't try to change the value of the string literal.

The blue const is your promise to the compiler that you won't try to change the pointer that points to the string literal (which would strand the literal in memory with no way of accessing it).

@gfvalvo
The red const is superfluous, because declaration/initialization makes the string by definition immutable.
The blue const makes sense, because we promise not to change the address of the pointer.
Anyway: char const *var = "some string" silences the warning.
Thanks all.

pagemaker:
@gfvalvo
The red const is superfluous

Obviously, it's not since omitting it provokes a compiler warning.

Then it's a little mystery that this: char const *var = "some string" silences the warning.

pagemaker:
Then it's a little mystery that this: char const *var = "some string" silences the warning.

char const *var is the same as const char *var. It's not the same as char *const var.

Changing 'char *' to 'const char *' does NOT 'make the data constant again'. The data is always constant, so you need a pointer that points to constant data.

The data type of a string constant is always 'const char *', so your variable that you assign to should be the same type.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.