Start Back Next End
  
ZBasic Language Reference
88
ZBasic Microcontrollers
BasicX Compatibility Note
Bit and Nibble data types are not available in BasicX compatibility mode.
3.24.1 Forcing Byte Alignment
As described earlier, sub-byte type variables are normally sub-allocated within a host variable.  One
consequence of this space-saving strategy is that a sub-byte type variable is often not aligned on a byte
boundary, a circumstance that imposes limitations on the possible uses of the variable.  One solution to
this problem is to instruct the compiler to align a particular variable on a byte boundary.  This is done by
adding the ByteAlign attribute to the variable definition as shown below.
Dim ByteAlign ba(1 to 20) as Bit
Each variable that is so defined will occupy an integral number of bytes of memory.  For the example
above, three bytes will be allocated even though only 20 of the 24 available bits will actually be used.
Note that the ByteAlign attribute may be used with any variable type but it has no effect for those types
that are inherently byte-aligned.  Also, the ByteAlign attribute may be used in a Structure to force sub-
byte types to be byte-aligned.
3.25 Structures
It is often useful to define a data type that is a collection of data elements.  For example, if you write a
program to manipulate dates it may be useful to define a data type that contains “year”, “month” and “day”
elements.  This is convenient because it allows you to think about or refer to the group of data elements
as a single entity rather than as the individual constituent elements.
In ZBasic, as in many other programming languages, such a collection of data items is referred to as a
“structure”.  A structure is a user-defined data type similar in some respects to an enumeration.  As with
an enumeration, you define a structure by specifying the consituent elements.  The syntax for defining a
structure at the module level is:
[Public | Private] Structure <name>
<member-definition>
...
End Structure
If neither Private nor Public is specified, the structure definition is public.  The ellipsis (…) in the
syntax above connotes that there may be zero or more additional member definitions.  A structure
definition must have least one member and may have a practically unlimited number of members.
A structure may be defined within a subroutine or function, either at the outer level or within any inner
block.  In this case, the Public and Private keywords have no useful purpose and are therefore not
allowed.
After a structure has been defined, the structure name may be used as a <type> in a variable or
structure definition.  A structure may also be used as the <type> in the formal parameter list of a
subroutine or function definition.  Note, however, that a Public subroutine or function cannot be defined
with a parameter that is a Private Structure.
A <member-definition> has the same syntax as that used to define a variable.  As with an ordinary
variable, a member may be a single data element or it may be an array.  The syntax for a member
definition is given by the two descriptions below – the first being for a non-array member and the second
being for a member that is an array.
Previous page Top Next page