whole-array_20operations_20in_20structures
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| whole-array_20operations_20in_20structures [2018/04/17 19:29] – Added syntax highlighting tbest3112 | whole-array_20operations_20in_20structures [2024/01/05 00:21] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| =====Whole-array operations in structures===== | =====Whole-array operations in structures===== | ||
| - | //by Richard Russell, August 2013//\\ \\ | + | //by Richard Russell, August 2013// |
| + | |||
| + | (for an alternative way of tackling this issue, see [[variable_length_array_structure|Variable-length array in a structure]]) | ||
| + | |||
| + | As it states in the main [[http:// | ||
| + | |||
| + | Usually these limitations are not serious, for example to initialise the contents of an array one can simply use a loop: | ||
| <code bb4w> | <code bb4w> | ||
| DIM struct{array(3, | DIM struct{array(3, | ||
| Line 10: | Line 17: | ||
| NEXT R% | NEXT R% | ||
| </ | </ | ||
| - | However there may be occasions when it would be convenient to be able to use the whole-array operations. Fortunately there is a way, although it has a few restrictions. By calling the procedure **PROC_arrayalias** (listed below) it is possible to create a conventional array which is an //alias// of an array in a structure. If a whole-array operation is performed on this alias array, the array in the structure is also affected, and if the structure array is modified the alias array also changes.\\ \\ | + | However there may be occasions when it would be convenient to be able to use the whole-array operations. Fortunately there is a way, although it has a few restrictions. By calling the procedure **PROC_arrayalias** (listed below) it is possible to create a conventional array which is an //alias// of an array in a structure. If a whole-array operation is performed on this alias array, the array in the structure is also affected, and if the structure array is modified the alias array also changes. |
| + | |||
| + | So for example an equivalent functionality to the code above may be achieved as follows: | ||
| <code bb4w> | <code bb4w> | ||
| DIM struct{array(3, | DIM struct{array(3, | ||
| Line 16: | Line 26: | ||
| alias() = -1 | alias() = -1 | ||
| </ | </ | ||
| - | Note the use of dummy variables **nil%** to 'pad out' the procedure call to the correct number of parameters. It is possible to create up to four - expandable to nine, see below - alias arrays in this way, each one corresponding to a different array within the structure (non-array members are ignored in this process). So for example:\\ | + | Note the use of dummy variables **nil%** to 'pad out' the procedure call to the correct number of parameters. It is possible to create up to four - expandable to nine, see below - alias arrays in this way, each one corresponding to a different array within the structure (non-array members are ignored in this process). So for example: |
| + | |||
| <code bb4w> | <code bb4w> | ||
| DIM test{a%(0), b%, c#(3,2), d$, e$(1)} | DIM test{a%(0), b%, c#(3,2), d$, e$(1)} | ||
| PROC_arrayalias(test{}, | PROC_arrayalias(test{}, | ||
| </ | </ | ||
| - | Here the three arrays **one%()**, **two#()** and **three$()** become aliases of the structure members **test.a%()**, | + | Here the three arrays **one%()**, **two#()** and **three$()** become aliases of the structure members **test.a%()**, |
| + | |||
| + | As stated previously, any of the whole-array operations (except SWAP) may be performed on the alias arrays, even something as simple as determining the dimensions: | ||
| + | |||
| <code bb4w> | <code bb4w> | ||
| DIM test{a%(0), b%, c#(3,2), d$, e$(1)} | DIM test{a%(0), b%, c#(3,2), d$, e$(1)} | ||
| Line 28: | Line 42: | ||
| maxcol% = DIM(two#(), 2) | maxcol% = DIM(two#(), 2) | ||
| </ | </ | ||
| - | Sadly there are a few limitations to this technique:\\ | + | Sadly there are a few limitations to this technique: |
| * The structure cannot be declared using a prototype (although sub-structures can). | * The structure cannot be declared using a prototype (although sub-structures can). | ||
| Line 36: | Line 50: | ||
| * Sub-sub-structures are allowed only **before** the first array member. | * Sub-sub-structures are allowed only **before** the first array member. | ||
| * The memory layout of the structure changes, so (e.g.) it cannot be passed to an API function. | * The memory layout of the structure changes, so (e.g.) it cannot be passed to an API function. | ||
| - | \\ | + | |
| + | As a warning that the layout of the structure is changed the **DIM(struct{})** function, which gives the structure' | ||
| + | |||
| + | Here is the **PROC_arrayalias** procedure. Up to five more parameters (which must have the formal variables **E%**, **F%**, **G%**, **H%** and **I%**) may be added if there are more arrays in the structure. No other code changes are required. | ||
| <code bb4w> | <code bb4w> | ||
| DEF PROC_arrayalias(RETURN s{}, RETURN A%, RETURN B%, RETURN C%, RETURN D%) | DEF PROC_arrayalias(RETURN s{}, RETURN A%, RETURN B%, RETURN C%, RETURN D%) | ||
whole-array_20operations_20in_20structures.1523993349.txt.gz · Last modified: 2024/01/05 00:16 (external edit)