User Tools

Site Tools


finding_20the_20size_20of_20an_20array_20in_20a_20structure

Differences

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

Link to this comparison view

finding_20the_20size_20of_20an_20array_20in_20a_20structure [2018/03/31 13:19]
127.0.0.1 external edit
finding_20the_20size_20of_20an_20array_20in_20a_20structure [2018/04/17 16:14] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //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 [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin4.html#​dimfunc|DIM function]]. For example consider an array declared as follows:​\\ ​ //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 [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwin4.html#​dimfunc|DIM function]]. For example consider an array declared as follows:​\\ ​
 +<code bb4w>
         DIM array(3,4)         DIM array(3,4)
 +</​code>​
 The following functions return the values shown:​\\ ​ The following functions return the values shown:​\\ ​
 +<code bb4w>
         PRINT DIM(array()) ​   : REM prints 2         PRINT DIM(array()) ​   : REM prints 2
         PRINT DIM(array(),​ 1) : REM prints 3         PRINT DIM(array(),​ 1) : REM prints 3
         PRINT DIM(array(),​ 2) : REM prints 4         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:​\\ ​ 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 bb4w>
         DIM struct{member1,​ member2, array(3,4), member4}         DIM struct{member1,​ member2, array(3,4), member4}
         PRINT FN_structarraydim(^struct{},​ 3, 0) : REM prints 2         PRINT FN_structarraydim(^struct{},​ 3, 0) : REM prints 2
         PRINT FN_structarraydim(^struct{},​ 3, 1) : REM prints 3         PRINT FN_structarraydim(^struct{},​ 3, 1) : REM prints 3
         PRINT FN_structarraydim(^struct{},​ 3, 2) : REM prints 4         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:​\\ ​ 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 bb4w>
         DEF FN_structarraydim(P%,​ N%, D%)         DEF FN_structarraydim(P%,​ N%, D%)
         P% = !P%+4 : N% -= 1         P% = !P%+4 : N% -= 1
Line 18: Line 25:
         P%+=3:​REPEAT P%+=1:UNTIL ?P%=0         P%+=3:​REPEAT P%+=1:UNTIL ?P%=0
         IF D% THEN =P%!(D%*4+2)-1 ELSE =P%?5         IF D% THEN =P%!(D%*4+2)-1 ELSE =P%?5
 +</​code>​
 \\  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. \\  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.
finding_20the_20size_20of_20an_20array_20in_20a_20structure.txt ยท Last modified: 2018/04/17 16:14 by tbest3112