Using a #define to load an array

Is it possible to add something this to a header file:



then in a function have:

for(a=0;a>=NUM;a++) { MyVar[a]=NUMBERS; }

So that MyVar would be filled like:

MyVar[0]=3 MyVar[1]=11 MyVar[2]=4 MyVar[3]=6

Or is there an easier way....

Yes, it is.

Sorry Paul...I modified the post

Can you give me an example?

Your first question, about using #define NUM 4, and using NUM later, was possible.

What your asking now is not.

Thanks! What is a good way of loading an array with constants?

could I:

const int MyVar[]={3,11,4,6};

That will work. Only because the declaration and initialization are performed in one step.

const int myVar[4];
myVar = {3, 11, 4, 6};

will not work.

This is more advanced and deals with pointers and the memory in the array itself, but you could copy the memory from one array to another at once...

int MyVar[]={3,11,4,6};
const int NewVals[] = {4,1,4,1};

void setup()
      memcpy(MyVar, NewVals, sizeof(MyVar));


void loop()

memcpy copies NewVals data to MyVar (notice it isn't const in my example anymore). You have to be careful though that you don't copy too large (or sometimes too little) into the target array pointer using this method.

Thanks! I will do some experimenting