Convert flash string to char*

How to pass flash string to function that accepts char* ?

auto myString = F("helloooooow worlddddd");


function myFunction(char *text){
}

Note: I cannot use String class because the class doesn't implemented well in ATTiny.

Either provide an overloaded function that accepts type (const __FlashStringHelper *), or use ‘strncpy_P()’ to copy the flash string into RAM.

not working :’(

		typedef char *tinyString;


		void Command::response(const tinyString &text) {
			if (responseCallback != nullptr) responseCallback(text);
		}

		void Command::response(const __FlashStringHelper *text) {
			char buffer[20];
			strcpy_P(buffer, text); // <== compile error
			response(text);
		}

YunuZ:
not working :cry:

That's a totally useless problem statement.

Post complete code.

Post complete error message.

			char buffer[20];
			strcpy_P(buffer, text); // <== compile error
			response(text);

gfvalvo:
Post complete code.

Post complete error message.

Which part of that didn't you understand?

C:\Users\Pesona Fashion\Documents\Arduino\libraries\Marco.Communications.Command\Marco.Communications.Command.cpp: In member function 'void Marco::Communications::Command::response(const __FlashStringHelper*)':

C:\Users\Pesona Fashion\Documents\Arduino\libraries\Marco.Communications.Command\Marco.Communications.Command.cpp:566:25: error: cannot convert 'const __FlashStringHelper*' to 'const char*' for argument '2' to 'char* strcpy_P(char*, const char*)'

    strcpy_P(buffer, text);

                         ^

This is how I’ve done it:

void response(const __FlashStringHelper *text) {
  const byte textLength = FSHlength(text);
  char buffer[textLength + 1];
  memcpy_P(buffer, text, textLength + 1);  //+1 for the null terminator
  response(buffer);
}

unsigned int FSHlength(const __FlashStringHelper * FSHinput) {
  PGM_P FSHinputPointer = reinterpret_cast<PGM_P>(FSHinput);
  unsigned int stringLength = 0;
  while (pgm_read_byte(FSHinputPointer++)) {
    stringLength++;
  }
  return stringLength;
}

Maybe there is a better way.

YunuZ:

		typedef char *tinyString;

What is that all about?

works. thank you.

typedef char *tinyString;

^^ just a syntax sugar
the tinyString is more human readable than char*

You’re welcome. I’m glad if I was able to be of assistance.

Heres another way

const char myString[] PROGMEM = "this is my String";  //string saved in flash
char   a;


a=pgm_read_byte(myString);

Serial.print(a); //prints out a byte in a string