next up previous contents
Next: Assignment Operators Up: The MPD Programming Language Previous: Variables and constants   Contents

Subsections

References and Dereferences

Subscripting and Slicing

Array variables or constants may be subscripted or sliced. A subscript is an expression that is evaluated to get the index of a single element while a slice is a subarray of an array i.e. the array formed by all elements between the given lower and upper bound of the slice inclusive. The general form is a list of bounds:

bound, bound, ...

where each bound is a single expression (a subscript) or a slice. Slice definitions has one of four forms.

expr : expr       expr : *       * : expr       *

The first form specifies the lower and the upper bound of the slice, the second form specifies the lower bound and takes the end of the array as the upper bound, the third form takes the beginning of the array as the lower bound and specifies the upper bound, while the fourth and last form takes the beginning and the end of the array as lower and upper bounds.

Substrings

Strings can be treated as one dimensional arrays of characters indexed from 1 to the size of the string. Subscripting and slicing for strings is identical to subscripting and slicing for arrays.

Specifying Record and Union Fields

The value of a field may be obtained using the dot ( . ) operator. For example, suppose that the record x is declared as follows

rec (string[10] name; int grade) x

The fields name and grade would then be referenced as x.name and x.grade resp. The syntax for unions is equivalent to the syntax for records.

Pointers

Dereferencing pointers, i.e. to obtain the value the pointer is pointing on, is done using the hat (^) operator. Assume the following pointers

ptr int p                 # a pointer to an integer
ptr ptr int pp            # a pointer to a pointer to an int
ptr rec(int i1,i2) prec   # a pointer to a record

Some examples of dereferencing would be

p^       # the integer to which the pointer p points 
pp^      # the integer pointer to which the pointer pp points 
pp^^     # the integer to which pp^ points 
prec^    # the record to which prec points to

 

Warning!

It is illegal to dereference the null pointer, null, a pointer pointing to a variable that is out of scope or assign to a to a constant using a pointer.

More Complex Examples

Assume the following declarations.

type point      = rec(real x,y)
type prim_color = enum(red,blue,yellow)
type rectangle  = rec(point upleft,botright;
                      prim_color color;
                      string[20] label)

rectangle display[1:N]
ptr rectangle ptrect

Then sample references include the following:

display[3].color      # the color of the 3rd displayed rectangle
display[3].label[1:4] # the first four chars of the label of
                      # display[3]
display[3].upleft.x   # the x coordinate of the upleft point of
                      # display[3]
prect^.upleft.x       # the x coordinate of the upleft point of
                      # the rectangle to which prect points


next up previous contents
Next: Assignment Operators Up: The MPD Programming Language Previous: Variables and constants   Contents
David Sands 2003-09-05