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: code format="bb4w" DIM array(3,4) code The following functions return the values shown: code format="bb4w" PRINT DIM(array)   : REM prints 2 PRINT DIM(array, 1) : REM prints 3 PRINT DIM(array, 2) : REM prints 4 code 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: code format="bb4w" 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 code 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: code format="bb4w" 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 code

An alternative method is to create an **alias array**, as described at [], and then find the dimensions of that array in the usual fashion.