I am using AVR-GCC version 4.7.0, and when I attempt to create an array of strings in FLASH memory I get the error:
variable ‘menu’ must be const in order to be put into read-only section by means of ‘attribute((progmem))’
I am using this code:
const char menu0[] PROGMEM = "choice0";
const char menu1[] PROGMEM = "choice1";
const char menu2[] PROGMEM = "choice2";
const char menu3[] PROGMEM = "choice3";
const char menu4[] PROGMEM = "choice4";
const char menu5[] PROGMEM = "choice5";
const char *menu[] PROGMEM = {menu0, menu1, menu2, menu3, menu4, menu5};
I have already read Stack Overflow question C - how to use PROGMEM to store and read char array, but all the answers I see don't include the const
keyword which makes me believe that they were written before it was needed.
How does one fix this problem?
const char * const menu[] PROGMEM = {menu0, menu1, menu2, menu3, menu4, menu5};
was the answer.
Store data in flash (program) memory instead of SRAM. There's a description of the various types of memory available on an Arduino board. The PROGMEM keyword is a variable modifier, it should be used only with the datatypes defined in pgmspace. h.
Explanation: The PROGMEM Utility can only be used for variables which have “public” access to the entire program and conversely can be accessed by anyone, or is a static variable, which means they need to be defined outside a function and be static or non-static.
The F() macro does two things: uses PSTR to ensure that the literal string is stored in flash memory (the code space rather than the data space). However, PSTR("some string") cannot be printed because it would receive a simple char * which represents a base address of the string stored in flash.
To store stuff in SRAM you just need to create a variable and pop in your value. If you want many thing, use an array. If you want them addressable, then use the index to that array as the address.
Try
const char* const menu[] PROGMEM...
Thus the array itself is constant, not a mutable array of const char*
pointers, as it were in the original code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With