Arrays

From QB64 Wiki
Revision as of 12:05, 17 April 2010 by imported>Cyperium
Jump to navigation Jump to search

Arrays can be seen as a table of values stored in one variable. An array consists of a number of elements (or subscripts) where each element can store data.

Arrays in QuickBASIC 4.5 and QBASIC were limited to 32767 elements, while arrays in QB64 is limited to 2147483647 elements (over 2 billion).

When the 64-bit version of QB64 is implemented 9223372036854775807 elements will be the limit (but only on 64-bit systems).

An array starts at element 0 unless changed by OPTION BASE (which can set the start at 1), you can also define the start and end area by DIMensioning with a range (DIM array(5 TO 10)).

REDIM can be used to redimension an array, the information contained in the array before the REDIM will be lost...however;

QB64 has an addition called _PRESERVE which can be used in a REDIM statement in order to preserve the information in the array.


The memory of the array starts at:

DEF SEG = VARSEG(array(0))

address = VARPTR(array(0))

If OPTION BASE is used change the 0 accordingly.


Multiple dimensions are possible; in QuickBASIC up to 60 dimensions, in QB64 the number of dimensions possible is currently unknown.

A comma is used to seperate each dimension, as;

DIM array(100,100,100,100)

...for 4 dimensions.

OPTION BASE 1 will cause all dimensions to begin at 1, OPTION BASE 0 will cause all dimensions to begin at 0. The default is 0. DIMensioning with a range is possible in all dimensions.


Example of use: (REDIM without _PRESERVE erases the contents)

REM $DYNAMIC DIM array(20) array(10) = 24 PRINT array(10) REDIM _PRESERVE array(30) PRINT array(10) REDIM array(10) PRINT array(10)

24 24 0