Array in header file

I want to define a constant in the Arduino IDE and then include a .h file so an array in the .h file is created and is the size of the constant I defined. Can this be done?

In .ino file:

#define ArrSz 16
#include <wow.h>

in .h file:

uint8_t  myArray[ArrSz];

dynamic arrays are typically done with malloc() - google for the details how it is used.

As long as you are just working within the one .ino and .h file, and you're not expecting that .h file to form part of a library (i.e., you're not wanting to change the size of an array in a library, but only one used in your own .ino file) then that would work fine.

If you want to pass the array size to a library, then you will have to think again. When the library is compiled (which is done separately to the .ino file) the #define won't exist.

In situations like that it is normal to either pass a size as a parameter to the constructor or .begin() and use malloc() to reserve the memory, or to create the array in your .ino and pass the pointer to it to the library's constructor / .begin().

You hit the nail majenko. I want to change a library array depending on the programmers value. I have a .begin and I will look at the malloc() method.

Thanks all.

ljbeng:
You hit the nail majenko. I want to change a library array depending on the programmers value. I have a .begin and I will look at the malloc() method.

Thanks all.

malloc() is risky if you don't know what you're doing. I have been using malloc() for 20 years, and I still shy away from it :wink:

Better to define the array in your main sketch and pass it (as a pointer) to the library.