CIE AS & A Level Computer Science, section 13.1 (User-defined data types)
Why user-defined types
Built-in types (INTEGER, STRING and so on) do not always describe the data well. A user-defined data type is one the programmer defines for a particular program. A non-composite type is one that is not built from other data types, so it holds a single value.
Enumerated type
An enumerated type defines a fixed list of named values. The values are ordered (the first is 0, the next is 1, and so on), and a variable of that type can only hold one of the listed values.
TYPE Season = (Spring, Summer, Autumn, Winter)
DECLARE ThisSeason : Season
ThisSeason ← Autumn
Benefits: the code is easier to read than using numbers or strings, and the language can reject invalid values, so there are fewer errors.
Pointer type
A pointer is a variable whose value is the memory address of another variable. The type of thing it points to is part of its definition.
TYPE IntPointer = ^INTEGER
DECLARE MyPointer : IntPointer
DECLARE Number : INTEGER
Number ← 42
MyPointer ← ^Number // the address of Number
OUTPUT MyPointer^ // follow the pointer: outputs 42
MyPointer^ ← 50 // changes Number to 50
^Number gives the address; MyPointer^ follows the pointer to the value stored there. Pointers are used to build linked lists, trees and other dynamic structures.
Common mistake: confusing a pointer with the value it points to. A pointer holds an address, not the data. Also, an enumerated type holds names, not strings: Autumn is not the same as "Autumn".
Exam tip: when asked to define a type, write the TYPE line exactly, then show a DECLARE. For a pointer, say it stores the address of a variable of a particular type.
Check yourself
What is a non-composite data type?
A type that is not made up of other data types, so each variable holds a single value.
What does a pointer variable store?
The memory address of another variable.
Give one advantage of an enumerated type.
The code is easier to read, and invalid values are rejected.