User manual
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
213
Structures
A structure represents a heterogeneous set of elements. Each element is called a member; the declaration of a structure
type species 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 identier, each type denotes a type, and each member is a valid identier. The scope
of a member identier is limited to the structure in which it occurs, so you don’t have to worry about naming conicts
between member identiers and other variables.
For example, the following declaration creates a structure type called Dot:
structure Dot
dim x as oat
dim y as oat
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 dened structure type. For example:
‘ Structure dening a circle:
structure Circle
dim radius as oat
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 dened 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