13.1 User-defined data types

Bulk view disabled for Guests. View lessons individually.

User-Defined Data Types: Enumerated and Pointer

A user-defined data type is a data type created by the programmer because no built-in type describes the data well. Two kinds are required by this syllabus: enumerated and pointer. Both are non-composite — they are defined without reference to another type.

1. Composite and Non-Composite Types

CategoryMeaningExamples
Non-compositeDefined without reference to another data typeEnumerated, pointer
CompositeBuilt from other data typesRecord, set, class, array

2. Enumerated Data Types

An enumerated type defines a new type by listing every value it may take. The values are ordered identifiers, not strings and not numbers.

TYPE TDay = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) TYPE TSuit = (Hearts, Diamonds, Clubs, Spades) // Declaring and using a variable of that type DECLARE Today : TDay Today ← Wednesday IF Today = Saturday OR Today = Sunday THEN OUTPUT "Weekend" ENDIF
Note the absence of quotation marks. Wednesday is an identifier belonging to the type TDay, not the string "Wednesday". Assigning Today ← "Wednesday" is a type error.

Why use one?

  • Only valid values can be assigned, so the compiler catches errors that a string or integer would allow
  • The code documents itself — Today = Saturday reads better than Today = 6
  • The values have a defined order, so they can be compared and used as loop counters
  • They are stored efficiently, typically as small integers internally
An enumerated type is not a set of strings. Answers describing it as "a list of words" or "an array of names" do not gain credit. It defines the complete set of permitted values for a new type.

3. Pointer Data Types

A pointer holds a memory address rather than a data value. The pointer type declaration states what type of data lives at that address.

TYPE TIntPointer = ^INTEGER // ^ means "pointer to" DECLARE MyPointer : TIntPointer DECLARE Value : INTEGER Value ← 42 MyPointer ← @Value // @ means "address of" OUTPUT MyPointer^ // dereference: outputs 42, the value AT the address OUTPUT MyPointer // outputs the address itself, e.g. 2001
SymbolMeaningRead as
^ in a type definitionDeclares a pointer to that type"pointer to"
@Gives the memory address of a variable"address of"
^ after a pointer variableAccesses the value stored at that address"the value pointed to by"

A worked picture

Memory +----------+----------+ Address | 2001 | 3050 | +----------+----------+ Holds | 42 | 2001 | +----------+----------+ ^Value ^MyPointer MyPointer = 2001 // the address MyPointer^ = 42 // the value at that address
Why pointers matter. They make dynamic data structures possible. A linked list, stack, queue or binary tree is built from nodes that each store a pointer to the next node, so the structure can grow and shrink while the program runs rather than having a fixed size decided in advance.

Pointers in a linked list node

TYPE TNode DECLARE Data : INTEGER DECLARE NextNode : ^TNode // points to another node of the same type ENDTYPE

This is how a chain of nodes is formed: each node's pointer holds the address of the following node, and the last node's pointer is set to null to mark the end of the list.

Advantages and risks

AdvantagesRisks
Enable dynamic structures that grow and shrink at run timeA pointer to a freed or invalid address causes unpredictable behaviour
Memory is allocated only as neededMemory leaks occur if allocated memory is never released
Passing a pointer avoids copying a large data structureHarder to read and debug than direct variable access

4. Exam Focus

Distinguish the pointer from the value it points to. MyPointer is an address; MyPointer^ is the data. Omitting the dereference symbol is the single most common error in pointer questions.
Use the TYPE … declaration correctly. A user-defined type must be defined before a variable of that type is declared. Marks are given for both steps separately, so show both.
Say why the type was chosen. Questions asking you to justify an enumerated type want the restriction of values and the resulting readability and error prevention — not simply "it is tidier".

Quick self-check

  • State the difference between a composite and a non-composite data type, with an example of each.
  • Define an enumerated type for the four seasons and declare a variable of that type.
  • Explain what a pointer stores.
  • Given P is a pointer to an integer holding 17, state the value of P and of P^.
  • Explain why pointers are necessary to implement a linked list.
  • Give one risk of using pointers.