Ad Space — Top Banner

3 Subscript wrong

Sinclair ZX Spectrum

Severity: Minor

What Does This Error Mean?

Error 3 on the ZX Spectrum means an array subscript is out of range — you tried to access an array element that does not exist. Check that your array was DIM'd large enough and that the index variable stays within bounds.

Affected Models

  • Sinclair ZX Spectrum 48K
  • Sinclair ZX Spectrum 128K
  • Spectrum+
  • Spectrum +2
  • Spectrum +3
  • ZX Spectrum Next
  • Fuse emulator

Common Causes

  • Array index is higher than the DIM size (e.g. DIM A(10) but accessing A(11))
  • Array index is zero or negative — Spectrum arrays are 1-based
  • Loop variable exceeds array bounds inside a FOR loop
  • Array not DIM'd at all before use

How to Fix It

  1. Check the DIM statement for the array and compare it to the highest index you access.

    DIM A(10) creates elements A(1) through A(10). Accessing A(0) or A(11) causes error 3. Spectrum BASIC arrays are 1-based — there is no element zero.

  2. Make sure the DIM is large enough for all the data you need to store.

    If your program uses a loop FOR I = 1 TO 20 : LET A(I) = I, then A must be DIM'd as at least DIM A(20).

  3. Add a bounds check before accessing the array.

    Before accessing A(I), add: IF I < 1 OR I > 10 THEN PRINT "OUT OF RANGE" : STOP This prevents the error and helps identify when an out-of-bounds access would have occurred.

  4. Remember that Spectrum string arrays work differently — each element is a fixed-length string.

    DIM A$(10, 5) creates 10 strings each 5 characters long. Accessing A$(11, 1) or A$(1, 6) will both cause error 3.

Frequently Asked Questions

Why are ZX Spectrum arrays 1-based rather than 0-based?

Sinclair designed Spectrum BASIC for non-programmers. Starting arrays at index 1 matches natural human counting — element 1 is the first element. This is different from C and most modern languages where arrays start at 0.

Can I use a two-dimensional array on the ZX Spectrum?

Yes. DIM A(5, 4) creates a 5 by 4 array. You access it with A(row, column), for example A(3, 2). The Spectrum supports multi-dimensional numeric and string arrays.