4

I'm trying to copy string from Flash to RAM but it results into garbage.

const char string[] PROGMEM  = "ABCDEF";
char buffer[20];
char *ptr = (char*)pgm_read_word(&(string));
strcpy_P(buffer, ptr);

What did I miss?

Luc-Olivier
  • 3,715
  • 2
  • 29
  • 29
  • 1
    You should use `pgm_read_ptr()`, standing to the [reference](http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html#ga5749897c91c479d02054fc02128de482). – Patrick Trentin Jul 13 '16 at 22:12
  • 1
    What context is `string[]` in? (An answer in the current state would require [psychic debugging](https://meta.stackexchange.com/questions/186035/policy-and-rationale-for-making-close-votes-invisible-to-most-users/186047#186047).) *[PROGMEM](https://reference.arduino.cc/reference/en/language/variables/utilities/progmem/)* says *"...variables must be either* ***globally*** *defined, OR defined with the `static` keyword, in order to work with PROGMEM"* – Peter Mortensen Jan 30 '23 at 18:14

1 Answers1

0
memcpy_PF(buffer, ((uint_farptr_t)string),sizeof(string));
iknow
  • 8,358
  • 12
  • 41
  • 68
Jacob
  • 1
  • 1
    Please refrain from answering with code-only. Note that you are not only answering to the OP, but also to any future readers of the question. Thus, please edit the post to contain an explanation as to why this line of code works. – José María Aug 12 '20 at 19:14