using reinterpret_cast and the F() macro

Hi there,

I am looking for an elegant way to store text in program memory.
This text shall then be copied to a buffer, for manipulating, and displayig on LCD.

So far I have come up with

strcpy_P (buffer, reinterpret_cast<const char*> (F("text stored in progmem")));

This works.
The F() macro takes care of storing in program memory.
However the strcpy_P likes a data type of const char, hence the reinterpret cast.
Strcpy_P is like strcpy, exept it accepts data coming from progmem.

To be honest, I don't understand completely what I am doing here.

For example, I thought I could use strcpy instead of strcpy_P, and then re-cast the data to char*
instead of const char*.

However the compiler then complains:

error: reinterpret_cast from type 'const __FlashStringHelper*' to type 'char*' casts away qualifiers

I understand I'm casting away the const qualifier, but that is exactly what I intend. For which reason does the compiler not allow this?

I'm also a bit afraid of using reinterpret_cast. Is this safe in the above context?

Thanks
Thomas

When a function argument is declared 'const' that is a promise from the function that it will not try to write into that argument. You can pass a non-'const' value for that argument. You are not required to pass a 'const' value.

Hi Delta_G,
thank you for your useful hint.
Just for reference, here is a functioning code line with strcpy_P:

char textbuffer[50]; // make this char array big enough for your longest string
strcpy_P (textbuffer, PSTR("text stored in progmem with PSTR macro"));
//do anything you like with the char array