# BBC BASIC Programmers' Reference

### Sidebar

finding_20the_20size_20of_20an_20array_20in_20a_20structure

## Finding the size of an array in a structure

by Richard Russell, January 2011

You can discover the size (i.e. the number of dimensions and the maximum subscript for each dimension) of a regular array using the DIM function. For example consider an array declared as follows:

`        DIM array(3,4)`

The following functions return the values shown:

```        PRINT DIM(array())    : REM prints 2
PRINT DIM(array(), 1) : REM prints 3
PRINT DIM(array(), 2) : REM prints 4```

The values are the number of dimensions, the maximum subscript of the 1st dimension and the maximum subscript of the 2nd dimension respectively.

Unfortunately you cannot use this method with an array which is a structure member. Instead you can use the function FN_structarraydim as follows:

```        DIM struct{member1, member2, array(3,4), member4}
PRINT FN_structarraydim(^struct{}, 3, 0) : REM prints 2
PRINT FN_structarraydim(^struct{}, 3, 1) : REM prints 3
PRINT FN_structarraydim(^struct{}, 3, 2) : REM prints 4```

The parameters of the function are a pointer to the structure, the 1-based index of the member in which you are interested (must be an array), and the dimension whose maximum subscript you want to know. If the third parameter is zero, the number of dimensions is returned.

Here is the FN_structarraydim function:

```        DEF FN_structarraydim(P%, N%, D%)
P% = !P%+4 : N% -= 1
WHILE N%:P%=!P%:N%-=1:ENDWHILE
P%+=3:REPEAT P%+=1:UNTIL ?P%=0
IF D% THEN =P%!(D%*4+2)-1 ELSE =P%?5```

An alternative method is to create an alias array, as described at http://bb4w.wikispaces.com/Whole-array+operations+in+structures, and then find the dimensions of that array in the usual fashion.

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
finding_20the_20size_20of_20an_20array_20in_20a_20structure.txt · Last modified: 2018/04/17 16:14 by tbest3112