Pages: [1]   Go Down
Author Topic: use of define  (Read 529 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

It works like a charm when using this syntax :
Code:
#define test 8
int first[test][test];
But not at all when using it in a function :
Code:
#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 !
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: