I'm currently changing some libraries to use flash instead of RAM for string storage so that I do not run out of SRAM on a project.
Some strings in the library are declared in this manner:
const char *testStringA = "ABC";
This is different to how I normally see this done:
const char testStringB[] = "DEF";
However, I think that these two are equivalent when declared const and initialised in the declaration. Both work fine in code.
I attempted to move these to flash:
const prog_char *testStringC PROGMEM = "GHI";
I then found that this didn't work. It was producing gobbledegook when printed.
However, following the more usual pattern of:
const prog_char testStringD[] PROGMEM = "JKL";
works fine.
I can see in the disassembly:
00000068 <testStringC>:
68: 04 01 ..
0000006a <_ZL11testStringD>:
6a: 4a 4b 4c 00 JKL.
So it is clear the pointer and PROGMEM results in the string/array not being initialised.
Why is this?
Example code:
#include <avr/pgmspace.h>
const int BUFFER_LEN = 20;
const char *testStringA = "ABC";
const char testStringB[] = "DEF";
const prog_char *testStringC PROGMEM = "GHI";
const prog_char testStringD[] PROGMEM = "JKL";
void setup()
{
Serial.begin(9600);
}
void loop()
{
char buffer[BUFFER_LEN];
Serial.println(testStringA);
Serial.println(testStringB);
strncpy_P(buffer, testStringC, BUFFER_LEN);
Serial.println(buffer);
strncpy_P(buffer, testStringD, BUFFER_LEN);
Serial.println(buffer);
delay(1000);
}