Re: Question about the usage of F()

F() casts a PROGMEM character string as type __FlashStringHelper. .print() uses that as a cue to know that the argument is a disguised PROGMEM character string. If you pass that to a function that takes a regular character pointer you SHOULD get a compiler error (__FlashStringHelper is not a pointer). To make your function work I think you need to write it like this:

void repeaterPrint(int counter, __FlashStringHelper text)
{
  for (int i = 0; i < counter; i++)
  {
    Serial.println(text);
  }
}

void loop()
{
   repeaterPrint(5, F("Hello World"));
}

Similarly, to declare an array of PROGMEM strings that you can Serial.print() you need to use the __FlashStringHelper type:

__FlashStringHelper string_table[] = 
{
   F("String 1"),
   F("String 2"),
   F("String 3")
}

And I don't need the PROGMEM keyword for any of that?

The PROGMEM keyword is in the F() macro and in the Print object.

Delta_G:
And on the function example, if I want it to be able to work with either RAM or flash, I could just use an overload one with char* and one with __FlashStringHelper?

Yes, I think that will work.

avr-gcc seems to not like the F() syntax in the array initialization
__FlashStringHelper string_table[] =
{
F("String 1"),
F("String 2")
}
and complains
tst.cpp:25:8: error: statement-expressions are not allowed outside functions nor in template-argument lists

This is a snippet I used that works in 1.0:

const __FlashStringHelper* options = reinterpret_cast <const __FlashStringHelper *>
(
    "blahblahblah"
);

and calling Serial.print(options); prints it.

WizenedEE:
This is a snippet I used that works in 1.0:

const __FlashStringHelper* options = reinterpret_cast <const __FlashStringHelper *>

(
    "blahblahblah"
);




and calling Serial.print(options); prints it.

sorry i dont get it WizenendEE

void setup() {
  Serial.begin(57600);
  Serial.print(F("TEST "));
  
const __FlashStringHelper* options = reinterpret_cast <const __FlashStringHelper *> 
(
    "blahblahblah"
);
Serial.print(options);
}

void loop(){

}

gives here

TEST $/???O????ï?ÿ?@?Æ

Interesting. I do remember having some problems with it, which I guess were related to it not actually putting it in the program memory. That's something to work off of, though.

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