Skip to content

Commit 5aefed2

Browse files
LeisureLadid-a-v
authored andcommitted
Update PROGMEM.rst (esp8266#6872)
* Update PROGMEM.rst Include array of strings in the description, since the examples in Arduino do not consider the right pointer size * Update PROGMEM.rst
1 parent e08b22c commit 5aefed2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

doc/PROGMEM.rst

+36
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,42 @@ the value back.
237237
}
238238
}
239239
240+
How do I declare Arrays of strings in PROGMEM and retrieve an element from it.
241+
------------------------------------------------------------------------------
242+
243+
It is often convenient when working with large amounts of text, such as a project with an LCD display, to setup an array of strings. Because strings themselves are arrays, this is actually an example of a two-dimensional array.
244+
245+
These tend to be large structures so putting them into program memory is often desirable. The code below illustrates the idea.
246+
247+
.. code:: cpp
248+
249+
// Define Strings
250+
const char string_0[] PROGMEM = "String 0";
251+
const char string_1[] PROGMEM = "String 1";
252+
const char string_2[] PROGMEM = "String 2";
253+
const char string_3[] PROGMEM = "String 3";
254+
const char string_4[] PROGMEM = "String 4";
255+
const char string_5[] PROGMEM = "String 5";
256+
257+
// Initialize Table of Strings
258+
const char* const string_table[] PROGMEM = { string_0, string_1, string_2, string_3, string_4, string_5 };
259+
260+
char buffer[30]; // buffer for reading the string to (needs to be large enough to take the longest string
261+
262+
void setup() {
263+
Serial.begin(9600);
264+
Serial.println("OK");
265+
}
266+
267+
void loop() {
268+
for (int i = 0; i < 6; i++) {
269+
strcpy_P(buffer, (char*)pgm_read_dword(&(string_table[i])));
270+
Serial.println(buffer);
271+
delay(500);
272+
}
273+
}
274+
275+
240276
In summary
241277
----------
242278

0 commit comments

Comments
 (0)