can we use P macro defined in string operations like strcpy(buffer,P("arduino"))

I want to know whether using P macro (" defined above setup code") like below

char p_buffer[350];
#define P(str) (strcpy_P(p_buffer, PSTR(str)), p_buffer)
#define pgmstr(x) (__FlashStringHelper*)(x)  //used for pointing the char arrays saved in flash memory very important

can be used in string functions like strcpy,strcmp

char buffer[100];
strcpy(buffer,P("hi all"));
strcat(buffer,P("Please help"));

I tried F macro inside string function but compiler gives error and P macro compiler complies the code properly and works. I just want to know wheter P macro can be used like above . how does F macro and P macro differs.

strcpy_P(buffer,PSTR("hi all"));

PSTR exists

thanks for your reply.

but does strcpy(buffer,P("is correct"));

Pratik_bhagat:
thanks for your reply.

but does strcpy(buffer,P("is correct"));

yes, but why would you copy the string twice?

I actually was running out of ram i am using arduino uno. i found that we can save const messages in flash. hence search the net and found this macro. Now I have used strcpy(buffer,P(""")); many times and in my code. MY code was hanging from last two days. the problem seems to different.

I didnt knew PSTR() i used F() and got error. So decided to use P() macro

Post the (complete!) error. F() macro is working perfectly fine here in most instances :slight_smile: But yeah, not with things like strcpy().

char buffer[100];
strcpy(buffer,P("hi all"));
strcat(buffer,P("Please help"));

expands to:

char p_buffer[350];
char buffer[100];
strcpy(buffer, strcpy_P(p_buffer, PSTR("hi all")));
strcat(buffer, strcpy_P(p_buffer, PSTR("Please help")));

which is equivalent to this but uses 350 more bytes of RAM:

char buffer[100];
strcpy_P(buffer, PSTR("hi all"));
strcat_P(buffer, PSTR("Please help"));

That P macro is ONLY useful when you are calling a function for which there is no "_P" version.

Note that the comma expression in the P macro does nothing. It is the LEFT side of the comma expression that is the value of the expression and strcpy() already returns the address of the destination buffer.