Compile error when changing an array to an const PROGMEM array

( I've put my question in an earlier topic but then realised that this is another problem.
I'm sorry. http://forum.arduino.cc/index.php?topic=574698.0 )

Now that I'm a bit further I stumbled into another problem...
I have a lot of arrays like this one and their bigger then showed here.

word Program_01[] = {0x0017, 0x0000, 0x0017, 0x7240, 0x3017};

Because there are a lot of them and they are constants, I want them to be turned into a const PROGMEM. So I turned it into:

const PROGMEM word Program_01[] = {0x0017, 0x0000, 0x0017, 0x7240, 0x3017};

In the rest of my code I used these lines to load that array into the next function.

 if (ProgramNumber==0x01) {LoadProgram(Program_01, sizeof(Program_01));}

void LoadProgram(word TheProgram[], int ProgramLength)
  {
  
  }

But after adding "const PROGMEM" I got this compile error:

warning: invalid conversion from 'const word* {aka const unsigned int*}' to 'word* {aka unsigned int*}' [-fpermissive] if (ProgramNumber==0x01) {LoadProgram(Program_01, sizeof(Program_01));}

note: initializing argument 1 of 'void LoadProgram(word*, int)' void LoadProgram(word TheProgram, int ProgramLength)

I don't really get where this goes wrong? Do I have to copy the array to RAM first before I can use it?

Here is the complete sketch which compiles ok. When you put
the const PROGMEM on front of one of the arrays in the top the problem shows up.
technischmuseum.nl/Temp/const_PROGMEM_fail.ino

When you use the ‘const’ keyword on a function argument declaration it is a promise by the function that it will not write into that argument. When you put the ‘const’ keyword on a variable declaration it tells the compiler that any attempt to write into that data is an error. You are getting a warning because you are passing a pointer to ‘const’ data to a function that is NOT promising not to try to write into that data. Add the ‘const’ keyword to the argument declaration to eliminate the warning.

Funny, I tried that and indeed the error message disappeared. So that was the right way?
But then I got all wrong readings... It looks like it takes data from another part of the memory.
Thanks so far, but I don't see my wrong readings yet... Maybe the pointer is out of range?

Are you using for PROGMEM read special instructions?

I have scaled down the program so that you can see exactly what goes wrong.

In this version the array prints out very nice on the serial monitor

technischmuseum.nl/Temp/test_OK.ino

In this version all data is corrupted. The only difference is
const PROGMEM in front of the array and const in front of the funcion variable

technischmuseum.nl/Temp/test_WRONG.ino

AWOL:
Are you using for PROGMEM read special instructions?

Not that I'm aware of? I made the code very simple now to show what goes wrong...

Did you answer the question in reply #3?

AWOL:
Did you answer the question in reply #3?

I hope so, now... I'm really not aware of anything special that I'm doing...

That is why I posted these two:

technischmuseum.nl/Temp/test_OK.ino
technischmuseum.nl/Temp/test_WRONG.ino

Maybe it is because you are NOT doing anything special.

A very friendly forum member pointed me in the direction of the direction of pgm_read_word_near. Thanks!

Proxxima:
A very friendly forum member pointed me in the direction of the direction of pgm_read_word_near. Thanks!

reply #3

AWOL:
reply #3

I’ve misunderstood that I think… I thought that you meant that if I have some sort
of special functions in my code. Which I didn’t have. I wasn’t aware of that
an array in PROGMEM needed a different way of reading…

Thanks anyway…