Const uint8_t flash array pointer problem

I have an Array - const uint8_t fan0[] PROGMEM = {
0xFF, 0x4A, 0x46, 0x49, .. . etc.

I declare a pointer - const uint8_t *ppoint;

I set the pointer to the array address - ppoint = fan0;

Compilation is OK and ppoint points to array fan0.
but if I try to pass it to a function, for instance sizeof, ppoint is 4 (4 bytes, the pointer size), but sizeof fan0 is 4026, the array size.

So I think the pointer type is incorrectly declared, or I need to tell the compiler its an array pointer.

Any help much appreciated.
Best Regards

Please show us a full sketch that we can try.

Which Arduino board do you use ? I assume it is a Arduino Mega.

A pointer to a PROGMEM array is not a normal pointer. You have to use the "pgm_read..." functions or other special functions to retrieve the data.
As soon as you turn that pointer into a normal pointer, then the data is lost.

The name of an array is also a label or a pointer to the array. There is a difference, because when the compiler uses the name of the array, then it knows the size of the array.
In most cases the "ppoint = fan0" is okay, as long as PROGMEM is not involved !

Have you read this:

I don't think it matters whether ppoint points to progmem or ram, sizeof will return the size of ppoint itself, not the size of what it points to.

A function has no way of determining the size of an array when it is passed a pointer, all it has is the address of the 1st element. Usually if you want the function to know the size you would need to pass that in the call to the function.

Its on an STM32F401. The address space is flat AFAIK. Integers are 32bit.
I want to pass various arrays to common functions for display, processing etc. But using a pointer doesn't seem to work.

Program wise using sizeof with results as above.
Serial1.print("sizeof ppoint = ");
Serial1.println("sizeof fan0 = ");

Best Regards

Further examination has made me realise its the sizeof function that's causing the problem. If I manually enter the array size and remove the sizeof function the array pointer works perfectly. It would be nice to get the sizeof function to return the correct array size, but if that cannot be done I can write a small piece of code to get all the array sizes with their original names and store the lengths in flash. Probably just make it run at compile time.

Thanks for your thoughts

Best Regards


Don't use any PROGMEM, F(), PSTR(), pgm_read_...() and ..._P() at all.
Make a sketch that shows the problem, and show us the sketch. Then we can help.

If you want to write compatible code that also runs on a Arduino Uno, then we can help, but you first have to fix the bug.

I think your question is this: Can I get the size of an array from a pointer ?
The answer is: No, not in plain good old 'C' code, and Yes, there are multiple ways in 'C++'.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.