array bounds must be constant - Printable Version +- Forum (https://www.boriel.com/forum) +-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11) +---- Forum: Help & Support (https://www.boriel.com/forum/forumdisplay.php?fid=16) +---- Thread: array bounds must be constant (/showthread.php?tid=314) array bounds must be constant - slenkar - 02-22-2011 How do I create an array with an integer instead of a literal number? I want to do this: Code:```DIM personX (numPersons) as ubyte DIM personY (numPersons) as ubyte DIM personHealth (numPersons) as ubyte DIM personWeapon (numPersons) as ubyte``` instead of: Code:```DIM personX (6) as ubyte DIM personY (6) as ubyte DIM personHealth (6) as ubyte DIM personWeapon (6) as ubyte```just in case I decide to change my mind about the number of persons Re: array bounds must be constant - boriel - 02-22-2011 Unfortunately this is not (yet) supported. What you're asking is a DYNAMIC ARRAY. This is a feature planned for 1.2.8 (or higher). Dynamic array are a bit trickier, slower and might take a little more memory, but on the other hand are more powerful and allow redimension during runtime. Meanwhile, you can create an array with a MAXPERSONS size (e.g. 20), and use a variable to store the real number of persons: Code:```CONST MaxPersons As Ubyte  = 20 DIM a(MaxPersons) As Ubyte LET numPersons = 10 : REM number of persons by now... FOR i = 1 TO numPersons     LET a(i) = RND * 10 NEXT i```And yes, you will allocate 10 unused positions. DYNAMIC Arrays are planned for the next release, please, be patient. Re: array bounds must be constant - slenkar - 02-22-2011 ah const, I didnt know we had that type thanks Re: array bounds must be constant - boriel - 02-22-2011 slenkar Wrote:ah const, I didnt know we had that type thanks You can also use #define (like a C preprocessor): Code:`#define MaxPersons 20`In fact, what's new in 1.2.7 is the complete new preprocessor, which allow powerful macros to be used as inline functions Code:```#define MyFunc(x, y)  \     PRINT x * y + 1; \     PRINT " is the result" MyFunc(3, 2)```The above program is literally converted to Code:```PRINT 3 * 2 + 1; PRINT " is the result"```And even much more complex examples! Re: array bounds must be constant - slenkar - 02-22-2011 inline functions are cool too Re: array bounds must be constant - compiuter - 02-23-2011 Hi, Can I use #define functions in fastcalls or subs? Re: array bounds must be constant - boriel - 02-23-2011 compiuter Wrote:Hi, Can I use #define functions in fastcalls or subs?Yes; #define are just MACROS. Wherever you invoke them, they will be *replaced* with its definition. Re: array bounds must be constant - compiuter - 02-26-2011 macros functions created with #define can contain embebed asm or only basic? Re: array bounds must be constant - boriel - 02-26-2011 compiuter Wrote:macros functions created with #define can contain embebed asm or only basic?Whatever! Of course, if you must use ASM...END ASM to use macros. Remember also to end each line with \ or _ character (line break continue), to tell the compiler those lines are macro ones: Code:```DIM a As Ubyte = 1 #define MyMacro  \     PRINT a; " MACRO START" \     ASM \     ld hl, _a \     inc (hl) \     END ASM \     PRINT a; " MACRO END" PRINT "HELLO WORLD" MyMacro```Notice line continuations "\". You can use "_" instead. You can think of macros like inline SUBs. Update: errr... :oops: Just discovered the ASM inside the macro wasn't being correctly supported!. This bug has been fixed and now it works. Please download r2109 Re: array bounds must be constant - compiuter - 03-08-2011 Code:```#define MyFunc(x, y)  \     PRINT x * y + 1; \     PRINT " is the result" MyFunc(3, 2)```I was testing and I can not compile macros with parameters. Re: array bounds must be constant - boriel - 03-08-2011 It works ok to me. :?: Which compiler version are you using? Download 1.2.7 or the latest devel, and try again... Re: array bounds must be constant - compiuter - 03-08-2011 Perhaps the version. I´ll take a look.