Error using F() macro

I have a project where I am trying to use the F() macro to save memory.

When I use

Serial.print(F("String to flashize"));
// and
strcpy(somebuf, "String to flashize");

everything is fine but when I use this:

strcpy(somebuf, F("String to Flashize"));

I get the following error:
error cannot convert 'const __FlashStringHelper*' to 'const char ' for argument '2' to 'char strcpy(char, const char)

I tried a cast but that did not work, though I might not have spelled it correctly.

you need something like

void Fcopy(char* buf, const __FlashStringHelper *ifsh)
{
  const char PROGMEM *p = (const char PROGMEM *)ifsh;
  int i = 0;
  uint8_t c  = 0;
  do
  {
    c = pgm_read_byte(p++);
    buf[i++] = c;
  } while ( c != 0 );
}

not tested but give it a try...

KeithRB:

strcpy(somebuf, F("String to Flashize"));

Read this: http://www.arduino.cc/en/Reference/PROGMEM
Use this:

strcpy_P(somebuf, PSTR("String to Flashize"));

Ideally, understand this: avr-libc: <avr/pgmspace.h>: Program Space Utilities

Thanks, that should do it.

Thanks for the refs, but in typical arduino fashion, neither describe the 'F()' macro or the strxxx_P() functions, just the strxxx_PF() functions.

in typical arduino fashion, [linked docmentation] doesn't describe the 'F()' macro or the strxxx_P() functions, just the strxxx_PF() functions.

Heh. In this case, it's not Arduino documentation that is at fault. (well, maybe the first link for not desribing F(); I think it pre-dates F()) The avr-libc documentation has changed recently, because of the way that pgmspace has changed in the newest versions of the compiler (note that Arduino does NOT use this newer version of the compiler, yet.) In the process, the documentation for the _P functions seems to have disappeared, even though they still exist. This is apparently a bug: AVR C Runtime Library - Bugs: bug #38025, Documentation misses memcpy_P et... [Savannah]

Meanwhile, you can download an older version of the documentation: http://download-mirror.savannah.gnu.org/releases//avr-libc/avr-libc-user-manual-1.4.0.pdf.bz2

Thanks gardner for the pointer to strcpy_P and PSTR( -- solved my issue with re-initializing some comm buffers without wasting RAM :slight_smile: