Structures

Use Struct/endStruct to define you own structures.

A structure consists of several different Data Types memory object using just one name.

Syntax (Declaration)

VISIBILITY

Declared structures are visible within their class. This means, one in the main program (base class „AVR“) declared structure is unknown within another class.

  • Up to version 2014.r2.4: If you want to swap data using a structure between classes, the structure declaration used for this purpose must be declared in the respective class again. The declarations must be the same, otherwise it causes memory leaks.
  • As of version 2015.r1 structure declarations are also usable with the class name. Is in a class a structure declared, it can also be used in the main program for the dimensioning of structures and vice versa. The above limitation to version 2014.r2.4 is thus canceled.
struct mystruct_t
  byte   value
  string text[20]
endstruct
 
dim var1 as mystruct_t          'the global structure type
dim var2 as myclass.mystruct_t  'the class-internal structure type
 
[...]
 
class myclass
  struct mystruct_t
    byte   value
    word   id
    string text[20]
  endstruct
 
  dim var1 as avr.mystruct_t  'the global structure type
  dim var2 as mystruct_t      'the class-internal structure type
 
  [...]
endclass

The structure must be declared before it can be accessed. The Declaration uses Struct/endStruct. The declaration does not use up memory. Memory is reserved after you have actually define a variable structure with Dim in in RAM or eeprom. You can declare a structure within another structure.

Structures themselves are static. This means that strings must always have a fixed length (same as when dimensioning in eeprom). Structures reserve as much memory in RAM or eeprom, as the sum of all elements declared.

SuperImposing

Structure declarations can also be used to simplify data access in Data Objects, or as a memory mask. See also: Pointer, chapter "SuperImpose"

Strings or characters arrays of dimensioned structures are automatically allocated as Pascal-String, e.g. the first byte in memory is the length byte. Take this into account when dimensioning a string. Example: A 10 byte long string needs 11 byte in memory.

Structures may be defined as an Array , thus making fields with several elemnts per array possible.

Example1:

[..]
' Declare Structure
struct point
  byte x
  byte y
endstruct
dim a(3) as point  ' Array with 4 elements of type "point"
a(0).x = 1
a(0).y = 20
a(1).x = 23
a(1).y = 42
[..]

Example2:

[..]
' Declare Structure
struct buffer
  byte   var(7)
  string text[4](2)  ' String as an array with 3 elements
endstruct
' Declare Structure
struct mydate
  byte    sec
  byte    min
  byte    hour
  byte    month
  word    year
  buffer  buf       ' nested Structure
  string  text[12]  ' Stringspeicher mit max. 11 characters (1 Length + 11 Data)
endstruct
 
dim d as mydate  ' Declare Structure
d.sec = 23
d.min = 42
d.hour = 12
d.mon = 2
d.year = 2012
d.text = "hello"
d.buf.var(4) = 123
d.buf.text(0)="ABC"
[..]