Nested Data Types

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
kirkkaf13@gmail.com
Posts: 5
Joined: Fri 23 Aug 2024, 21:37

Nested Data Types

Post by kirkkaf13@gmail.com »

Hello,

I know it's possible to have;
  • An array of structures:

    Code: Select all

    DIM Triangle{(2)points}
  • An array of structures and substructures:

    Code: Select all

    DIM Triangle{(2)points{x}}
  • A structure with an array:

    Code: Select all

    DIM Triangle{points(2)}
But how can I have a structure with an array of substructures?

Code: Select all

DIM Triangle{points{(2)x,y,z}}
Kirk
Richard Russell
Posts: 272
Joined: Tue 18 Jun 2024, 09:32

Re: Nested Data Types

Post by Richard Russell »

kirkkaf13@gmail.com wrote: Fri 23 Aug 2024, 21:48 But how can I have a structure with an array of substructures?

Code: Select all

DIM Triangle{points{(2)x,y,z}}
The short answer is that you can't. In the case of your specific example, where the array of substructures is the only member of the parent structure, you can of course simply do this:

Code: Select all

DIM Triangle_points{(2)x,y,z}
But I appreciate that your example was simplified for the purpose of illustration, and that what you would ideally like to do is probably:

Code: Select all

DIM Triangle{points{(2)x,y,z}, other_members}
But this isn't supported in BBC BASIC. The simplest workaround is to separate out the contents into two structures rather than one:

Code: Select all

DIM Triangle_points{(2)x,y,z}
DIM Triangle{other_members}
Personally I don't find this too much of a compromise. The only way to do 'better' (and that's debatable) is to store a pointer to the array of sub-structures in the main structure, similar to what is described in this Wiki article. You then end up declaring your structure as follows:

Code: Select all

DIM Triangle{points%%, other_members}
That technique was intended for use when the array member is 'variable length', but is equally applicable if it's an array of structures. Although the code is messy, the messiness is 'hidden away' in procedures (and could even be in a library).