The Hyper Programming Language

Pointer Types

General

Both C++ programmers and Java programmers should already be familiar with pointers. In C++, you declare and use pointers explicitly, while in Java any variable that is not of some primitive type is actually a pointer (but in Java they prefer to call it a reference). In C++ you can do very ugly things with pointers, and in Java you can only use it in a safe way.

First of all, pointers in Hyper are not like pointers in C++, and neither like in Java. Actually they are somewhere in between. They are declared explicitly, like in C++, but they are used implicitly most of the time. They are also type-safe, like in Java. In Hyper there is no such thing as pointer arithmetic. And you can view them as something in between pointers and references from C++.

Declaring pointers

Pointer declarations follow the 'left to right' rule. You write a `*' (an asterisk) to indicate that it is a pointer, and to the right of that you write the type the pointer points to. If you want the pointer to be constant, then put the const keyword before the asterisk. Note: declaring a pointer pointing to another pointer is not allowed.

Pointers in expressions

Most of the time, when you are not manipulating pointers, you don't have to think about their usage in expressions at all! They are automatically referenced and dereferenced. That's why you can't declare a pointer to another pointer. The compiler could never know when it would dereference to the first pointer or to the second. The lack of a pointer to another pointer is not a problem, you don't miss any functionality.

Because of the automatic dereferencing, you won't be able to assign a new value to a pointer with the standard assignment operator '='. You can't compare pointers with the normal equality and inequality operators either. So we have to use special pointer operators to do these things. Here are the pointer operators, listed with their non-pointer counterparts:

Operator Meaning
=$ Pointer assignment
- or -
Pointer equality
(depends on the context)
!$ Pointer inequality
Operator Meaning
= Assignment
- or -
Equality
(depends on the context)
!= Inequality

To assign an initial value to a variable or field, you use an initializer. The initializer is always placed behind a '=', which looks just like a normal assignment. However, for a pointer variable the initializer automatically serves as a pointer assignment.

Example

procedure p()
  var a : byte = 7
  var b : byte = 5 + 2
  var p : const * byte = a  # make p point to a
  var q : * byte = b  # make q point to b

  if p = q then  # compare the bytes p and q point to
    # this comparison will evaluate to true, because 'a' and 'b' have the same value
  end

  if p =$ q then  # compare the pointers
    # this comparison will evaluate to false, because 'p' and 'q' don't point to the same thing
  end

  if a =$ p then  # does p point to a ?
    # yes
  end

  q =$ p  # let q point to the same value as p

  if p = q then  # compare the bytes p and q point to
    # this comparison will still evaluate to true, because `a' has the same value as `a'
  end

  if p =$ q then  # compare the pointers
    # and this will also evaluate to true
  end
end

See also


Valid HTML 4.01 Strict Valid CSS!