Structures

A structure represents a heterogeneous set of elements. Each element is called a member; the declaration of a structure type specifies a name and type for each member. The syntax of a structure type declaration is

structure structname
  dim member1 as type1
  '...
  dim membern as typen
end structure

where structname is a valid identifier, each type denotes a type, and each member is a valid identifier. The scope of a member identifier is limited to the structure in which it occurs, so you don’t have to worry about naming conflicts between member identifiers and other variables.

For example, the following declaration creates a structure type called Dot:

structure Dot
  dim x as float
  dim y as float
end structure

Each Dot contains two members: x and y coordinates; memory is allocated when you instantiate the structure, like this:

dim m, n as Dot

This variable declaration creates two instances of Dot, called m and n.

A member can be of the previously defined structure type. For example:

' Structure defining a circle:
structure Circle
  dim radius as float
  dim center as Dot
end structure

Structure Member Access

You can access the members of a structure by means of dot (.) as a direct member selector. If we had declared the variables circle1 and circle2 of the previously defined type Circle:

dim circle1, circle2 as Circle

we could access their individual members like this:

circle1.radius = 3.7
circle1.center.x = 0
circle1.center.y = 0

You can also commit assignments between complex variables, if they are of the same type:

circle2 = circle1  ' This will copy values of all members
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code