# BBC BASIC Programmers' Reference

### Site Tools

sub-structure_arrays

# Differences

This shows you the differences between two versions of the page.

 — sub-structure_arrays [2020/12/19 10:59] (current)richardrussell created 2020/12/19 10:59 richardrussell created 2020/12/19 10:59 richardrussell created Line 1: Line 1: + =====Sub-structure arrays===== + + //by Richard Russell, December 2020// + + Sub-structure arrays (that is, a structure member that is itself an array of structures) are not supported in any current version of BBC BASIC. ​ For example one might prefer to have code something like this, but unfortunately it doesn'​t work: + + + DIM ss{a, b%, c\$} + DIM mystruct{ss{(9)}=ss{}} : REM Doesn'​t work + + FOR I% = 0 TO 9 + mystruct.ss{(I%)}.a = SQR(I%) : REM Doesn'​t work + NEXT + + FOR I% = 0 TO 9 + PRINT mystruct.ss{(I%)}.a : REM Doesn'​t work + NEXT + ​ + + Fortunately there is a reasonably neat workaround available: + + + DIM ss{a, b%, c\$} + DIM mystruct{ss&​(9,​DIM(ss{})-1)} + + FOR I% = 0 TO 9 + PTR(ss{}) = ^mystruct.ss&​(I%,​0) + ss.a = SQR(I%) + NEXT + + FOR I% = 0 TO 9 + PTR(ss{}) = ^mystruct.ss&​(I%,​0) + PRINT ss.a + NEXT + ​ + + This works by replacing the sub-structure array **ss{()}** by a two-dimensional byte array **ss&​()** of the same size.  Then, when the contents of one of the sub-structure array elements needs to be accessed, the pointer to the ss{} structure is set equal to the memory address of the matching element of the ss&() array. + + This takes advantage of an extension to the PTR() pseudo-variable and only works, as shown, in //BBC BASIC for Windows// version 6.14a or later, //BBC BASIC for SDL 2.0// version 1.15a or later, or //BBC BASIC Console Mode editions// version 0.26 or later.
sub-structure_arrays.txt ยท Last modified: 2020/12/19 10:59 by richardrussell