programming-languages:mql:struct
Struct
Structure objects allow saving data of different types.
Once the structure is defined, an instance of it must be created in order to access its elements.
struct Structure { double arr[]; int x; }; Structure a; // structure a.arr[0] = 2; a.x = 1; Structure b[]; // array b[0] = a; // copy the structure because each b[n] is empty b[1] = a; b[1].arr[0] = 4; b[1].x = 2; " b[1].arr[0] = ", b[1].arr[0], "; b[1].x = ", b[1].x); }
Structures can be used as structure of arrays (SoA) or as array of structures (AoS):
- SoA tends to offer better performances;
- AoS tends to offer better readability of the code.
struct Colour { double c[]; }; Colour colour; // structure of array (SoA) struct Shape { double s; }; Shape shape[]; // array of structure (AoS) soa(); aos(); } void soa() { // better performances } void aos() { // better clearness }
Important: it's not possible to declare a structure within a function and pass its instance to another one.
programming-languages/mql/struct.txt · Last modified: 2023/12/30 09:13 by tormec