using_20array_20pointers
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| using_20array_20pointers [2018/03/31 13:19] – external edit 127.0.0.1 | using_20array_20pointers [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| //by Richard Russell, December 2009//\\ \\ In //BBC BASIC for Windows// you can perform operations on **entire arrays**, such as arithmetic operations and the [[http:// | //by Richard Russell, December 2009//\\ \\ In //BBC BASIC for Windows// you can perform operations on **entire arrays**, such as arithmetic operations and the [[http:// | ||
| + | <code bb4w> | ||
| a() = a() + b() + c() | a() = a() + b() + c() | ||
| d() = e() * f() | d() = e() * f() | ||
| g() = h() . i() | g() = h() . i() | ||
| j() = 1, 2, 3, 4, 5 | j() = 1, 2, 3, 4, 5 | ||
| + | </ | ||
| This is fine so long as the arrays you want to operate on are known in advance, but suppose you have a large number of arrays and you want to determine **at run time** which is acted upon.\\ \\ Ideally what you might like to have is an **array of arrays**, then you could access the required array by means of an index, but //BBC BASIC for Windows// does not provide that facility. At first sight it would seem that a workaround would be to use an **array of structures**, | This is fine so long as the arrays you want to operate on are known in advance, but suppose you have a large number of arrays and you want to determine **at run time** which is acted upon.\\ \\ Ideally what you might like to have is an **array of arrays**, then you could access the required array by means of an index, but //BBC BASIC for Windows// does not provide that facility. At first sight it would seem that a workaround would be to use an **array of structures**, | ||
| + | <code bb4w> | ||
| number_of_arrays = 10 | number_of_arrays = 10 | ||
| DIM array_pointer(number_of_arrays-1) | DIM array_pointer(number_of_arrays-1) | ||
| + | </ | ||
| Next the individual arrays are DIMensioned; | Next the individual arrays are DIMensioned; | ||
| + | <code bb4w> | ||
| FOR index% = 0 TO number_of_arrays-1 | FOR index% = 0 TO number_of_arrays-1 | ||
| PROCdimarray(array_pointer(index%)) | PROCdimarray(array_pointer(index%)) | ||
| Line 15: | Line 20: | ||
| DEF PROCdimarray(RETURN array()) : DIM array(1,2) : ENDPROC | DEF PROCdimarray(RETURN array()) : DIM array(1,2) : ENDPROC | ||
| + | </ | ||
| Here the procedure is shown ' | Here the procedure is shown ' | ||
| + | <code bb4w> | ||
| DIM initial(1, | DIM initial(1, | ||
| initial() = 1, 2, 3, 4, 5, 6 | initial() = 1, 2, 3, 4, 5, 6 | ||
| Line 23: | Line 30: | ||
| DEF PROCinitarray(RETURN a(), i()) : a() = i() : ENDPROC | DEF PROCinitarray(RETURN a(), i()) : a() = i() : ENDPROC | ||
| + | </ | ||
| As before, the procedure is shown inline purely for convenience.\\ \\ If one wished to initialise the arrays differently then that can be achieved in a similar way:\\ \\ | As before, the procedure is shown inline purely for convenience.\\ \\ If one wished to initialise the arrays differently then that can be achieved in a similar way:\\ \\ | ||
| + | <code bb4w> | ||
| DIM initial(1, | DIM initial(1, | ||
| FOR index% = 0 TO number_of_arrays-1 | FOR index% = 0 TO number_of_arrays-1 | ||
| Line 29: | Line 38: | ||
| PROCinitarray(array_pointer(index%), | PROCinitarray(array_pointer(index%), | ||
| NEXT | NEXT | ||
| + | </ | ||
| Of course there' | Of course there' | ||
| + | <code bb4w> | ||
| dst% = 3 | dst% = 3 | ||
| src% = 6 | src% = 6 | ||
| Line 35: | Line 46: | ||
| DEF PROCaddarray(RETURN dst(), RETURN src()) : dst() += src() : ENDPROC | DEF PROCaddarray(RETURN dst(), RETURN src()) : dst() += src() : ENDPROC | ||
| + | </ | ||
| This adds the array with index **src%** to the array with index **dst%**.\\ \\ If you want to perform a calculation on an array which returns a scalar value, a convenient way is to use a function rather than a procedure: | This adds the array with index **src%** to the array with index **dst%**.\\ \\ If you want to perform a calculation on an array which returns a scalar value, a convenient way is to use a function rather than a procedure: | ||
| + | <code bb4w> | ||
| DIM result(number_of_arrays-1) | DIM result(number_of_arrays-1) | ||
| FOR index% = 0 TO number_of_arrays-1 | FOR index% = 0 TO number_of_arrays-1 | ||
| Line 42: | Line 55: | ||
| DEF FNmod(RETURN array()) = MOD(array()) | DEF FNmod(RETURN array()) = MOD(array()) | ||
| + | </ | ||
| Hopefully you can now see a pattern emerging. Whenever you want to perform an operation on one or more of the arrays, you must put that operation in a procedure (or function, if appropriate) and call that procedure or function with one or more parameters which are // | Hopefully you can now see a pattern emerging. Whenever you want to perform an operation on one or more of the arrays, you must put that operation in a procedure (or function, if appropriate) and call that procedure or function with one or more parameters which are // | ||
using_20array_20pointers.1522502388.txt.gz · Last modified: 2024/01/05 00:16 (external edit)