Using F( "string" ) as argument for calling a function

Hi,

First problem: My background is “C” and I am an Arduino-Newbie… :wink:

Second problem:

I have something like this:

void myprint( mystring ) {


u8glib.print( mystring )

}

void printer( void ) {

myprint ( F( “The String” ) );

}

The first problem I have to solve by myself, BUT … :wink:

For the second problem:
How do I have to construct the call and the argument to a function, which expects a string, if
I want to use the advantage of using F( “string”) instead of simply using “string” wile calling
that function?

Thank you very much in advance for any help!
Best regards,
mcc

have you checked the PROGMEM reference section - F() is just a special use case of storing things in flash (program) memory instead of SRAM

Hi J-M-L,

thanks for mention, that F() has something to do woth PROGMEM!
Will read that!

Best regards,
mcc

The F() macro is found in Wstring.h:

class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

It puts the string literal in FLASH and casts a pointer to it to type “class __FlashStringHelper *”. That class doesn’t actually DO anything but the various Print class recognizes an argument of that type and recognizes it as a pointer to a string in the FLASH address space. The Print class methods then fetch the string from FLASH and .write() it to the output.

If you want your functions that accept a string as a ‘char *’ to work with the F() macro you have to declare a second version that takes a 'class __FlashStringHelper *" instead of the ‘char *’. You then store the argument into a ‘char *’ and treat it as a pointer into FLASH.

See Print.cpp in the Arduino core sources for examples.