Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
Declaring Array Variables
◄Simple Variables► ◄Variables► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
Declaring Array Variables
An array is a group of objects referenced with the same variable name. The
individual values in an array are elements. Array elements are also
variables, and can be used in any BASIC statement or function that uses
variables. You "dimension" an array when you use it the first time or when
you declare the name, type, and number of elements in the array.
Each element in an array is referred to by an array variable subscripted
with an integer or an integer expression. (Noninteger numeric expressions
used as array subscripts are rounded to integer values.) An array-variable
name has as many subscripts as there are dimensions in the array. For
example, V(10) refers to a value in a one-dimensional array, while T$(1,4)
refers to a value in a two-dimensional string array.
The default upper subscript value for any array dimension is 10. The
maximum subscript value and the number of dimensions can be set by using
the DIM statement. The maximum number of dimensions for an array is 60.
The maximum number of elements per dimension is 32,767.
You may have arrays of any simple variable type, including records. To
declare an array of records, first declare the data type in a TYPE
statement and then dimension the array:
TYPE TreeNode
LeftPtr AS INTEGER
RightPtr AS INTEGER
DataField AS STRING * 20
END TYPE
DIM Tree(500) AS TreeNode
Each element of the array Tree is a record of type TreeNode. If the
array Tree is not part of a nested set of arrays of records, or if it
is the outermost array in a nested set of arrays of records, it may
be dynamic. For example:
TYPE TreeNode
LeftPtr AS INTEGER
RightPtr AS INTEGER
DataField AS STRING * 20
END TYPE
DIM Tree() AS TreeNode
To use a particular element of a record in an array, use the dot notation
form (variablename.elementname):
CONST MAXEMPLOYEES = 500
TYPE EmployeeRec
FullName AS STRING * 25
SocSec AS STRING * 9
END TYPE
DIM Employees(MAXEMPLOYEES) AS EmployeeRec
.
.
.
PRINT Employees(I).FullName;" ";Employees(I).SocSec
Note: Array names are distinct from simple variable names. The array
variable T and the simple variable T in the following example
are two different variables:
DIM T(11)
T = 2 : T(0) = 1 'T is simple variable.
FOR I% = 0 TO 10 'T(0) is element of array.
T(I% + 1) = T * T(I%)
NEXT
Array elements, like simple variables, require a certain amount of memory,
depending on the variable type.
To find the total amount of memory required by an array, multiply the
number of elements by the bytes per element required for the array type.
For example, consider the following two arrays:
DIM Array1(1 TO 100) AS INTEGER
DIM Array#(-5 TO 5)
Array1 has 100 integer elements, so its values take 200
bytes of memory. Array2 has 11 double-precision
elements, so its values require 88 bytes of memory. Because BASIC
must store information about the array along with the array's values,
arrays take slightly more memory than just the space for the values.
See Also ◄String Array Storage►
◄Numeric Array Storage►
◄Static and Dynamic Arrays►
◄Huge Dynamic Array Storage►