Go Down

Topic: use of define (Read 626 times) previous topic - next topic

ludo

I would like to set the size of array with a #define command.

It works like a charm when using this syntax :
Code: [Select]
#define test 8
int first[test][test];

But not at all when using it in a function :
Code: [Select]
#define test 8
int second(int parameter[test][test]) {}

 
The compilation failed and said : error: 'test' was not declared in this scop.

I do not unsterstand why. As the 2 dimensionnal array may vary from [8][8] to [20][70], I would like to save some memory at the compilation step.

Thanks for your help !

mellis

Ah, you've hit on a weird quirk caused by one of the few manipulations we do to your code before passing it to the C/C++ compiler (avr-gcc).  The Arduino software automatically generates prototypes for your functions.  This basically involves taking a copy of the function header and sticking it at the top of your sketch.  Unfortunately, this means that it ends up before the #define statement, and so test is undeclared at that point.

One workaround is to use a single dimensional array whose size is the product of the two dimensions you want, and use (y * X_SIZE + x) to access an element of it.  Then you can declare the function as:

int second(int parameter[])

and not need to use the #define'd value.  

ludo

That's a bit tricky, but I will work.
I can now finished to port a Game of Life on MAX7219.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy