Go Down

Topic: array of string-warning: ISO C++ forbids converting a string constant to 'char*' (Read 214 times) previous topic - next topic

ansb

Hi,

I have some char defined like this:
char _RECUSADA[] = "RECUSADA";
char _TERMINADA[] = "TERMINADA";


And I use them with a function defined like this:
void display_message(char* the_txt1)
{}


The call is like this:
display_message(_RECUSADA);
and it works fine.

But I want also, in some cases, use an index to display a string from an array.
So I have:
char* _PTR_INTER[] = {"INCENDIO","TRANSITO","DIVERSO"};

And I call my function like this:
display_message(_PTR_INTER[1]);

It works, but while compiling I have a warning:
warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
char *_PTR_INTER[] = {"INCENDIO","TRANSITO","DIVERSO"};

Any suggestion to avoid the warning?

Thanks a lot

PaulS

Quote
Any suggestion to avoid the warning?
Sure. Carefully read the message.

_PTR_INTER[ n ] is a pointer to a constant char array. Your function expects a non-constant pointer.

You can cast _PTR_INTER[ n ] to a char * (a non-constant pointer) OR, even better, you can modify your function to take a const char *.
The art of getting good answers lies in asking good questions.

ansb

Sure. Carefully read the message.

_PTR_INTER[ n ] is a pointer to a constant char array. Your function expects a non-constant pointer.

You can cast _PTR_INTER[ n ] to a char * (a non-constant pointer) OR, even better, you can modify your function to take a const char *.
Well I get the anwser. The problem is not the function but in a change in C++11 Standard (ISO/IEC 14882:2011).

char* _PTR_INTER[] = {"INCENDIO","TRANSITO","DIVERSO"}; is no longer possible.

You have to write:
char* const _PTR_INTER[] = {"INCENDIO","TRANSITO","DIVERSO"};


Go Up