Can I have an Array in a .cpp file and reference it from .ino sketch?

I need to have my sketch fill in some data into an array and have access to that array from both the sketch and the .cpp code.

I assume I could have the array in the sketch and pass an address of it some way. But that's not the best way to arrange it. It is far better (since this is reusable library code) to have the array in the library and have the sketch put data into it.

How can this be done?

This is what I need to have defined, preferably in the .cpp code:

byte CharBuf[300]; // Characters put here will be converted into bit maps later.
byte ShiftBuf[9][8]; // Holds the shifted bitmaps to put into the display by rows.

This code is currently in the sketch but needs to be globally accessed both from sketch and .cpp library code.

I tried dropping it into the public section in the .h file but it was not found by the sketch. Yes, I exited and reloaded the sketch to pick up changes.

public:

byte CharBuf[300]; // Characters put here will be converted into bit maps later.
byte ShiftBuf[9][8]; // Holds the shifted bitmaps to put into the display by rows.

Thanks,
Mike

Have a look at using the keyword "extern" to declare the array in your .ino file and let it know that the definition lies elsewhere.

That seems to have done it. I put it in three places before I got it right. Could not find a complete example. It finally worked by putting it in before the "class" statement in the .h file.

in the .h:

extern byte CharBuf[300]; // Characters put here will be converted into bit maps later.
extern byte ShiftBuf[9][8]; // Holds the shifted bitmaps to put into the display by rows.

class LedControl {
private :
// The array for shifting the data to the devices
byte spidata[16];

and putting these two in the .ino exactly the same but without the "extern" in front. I am able to print the same data from both the .ino and the .cpp file.

Thanks for the quick help. It lead me to making working code.

Mike

Philosophy vs. Reality of coding (this worked until I tried to really use it)

I was told in one thread that it is good to define (only) in .h files and populate in .cpp files. So I moved my big array from .h to .cpp. That works pretty well... until...

I needed to access the array from the .ino sketch.

Array definition (now in .cpp): PROGMEM const byte ABitmap8[136][8] = { a bunch of data };

Tried putting (in .ino): extern const byte ABitmap8[136][8];

Compile fails with these messages:

[path]\Time_on_LED_Matrix_8x8x8_Smooth_Scrolling.cpp.o: In function loop': [path]\Time_on_LED_Matrix_8x8x8_Smooth_Scrolling.ino:152: undefined reference to ABitmap8'
[path]/Time_on_LED_Matrix_8x8x8_Smooth_Scrolling.ino:152: undefined reference to `ABitmap8'
collect2.exe: error: ld returned 1 exit status
Error compiling.

on this statement: ShiftBuf*[j] = pgm_read_byte(&ABitmap8[CharOffset][j]);*
When it is in the .h file (frowned upon, I hear) all works just fine and I don't need the extern.
What to do... what to do... How can I do it "right" and have it work?
Mike

Is this just impossible with GCC/Arduino setup? Seems like it should work. What do I need to do?

I really would like to gain access to this array from the sketch. EXTERN is not working. What should I be doing?

Is this just impossible with GCC/Arduino setup?

No.

What do I need to do?

Write the code properly.

I really would like to gain access to this array from the sketch.

Permission granted.

EXTERN is not working.

Perhaps because the keyword is extern.

What should I be doing?

If you haven't guessed, we can NOT see your code.