Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An array of strings stored in flash with PROGMEM in Arduino

Tags:

c

arduino

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.

like image 402
Favil Orbedios Avatar asked Jan 14 '13 19:01

Favil Orbedios


People also ask

What is progmem in Arduino?

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.

What type of variables does the progmem utility work on?

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.

What is f () in Arduino?

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.

How does Arduino store data in SRAM?

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.


1 Answers

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.

like image 104
Anton Kovalenko Avatar answered Oct 21 '22 16:10

Anton Kovalenko