User-defined data types

Computer Science A2 · Data Representation. The idea in plain words, the rules to remember, the mistake to avoid and a worked example, written and checked by Mega Lecture.

Test this topic freeAll Computer Science A2 topics

The idea

Built-in types such as INTEGER and STRING cannot always describe the data of a real problem. A user-defined data type is designed by the programmer so that the program models the data closely, is easier to read and lets invalid values be caught.

Non-composite types do not refer to other types: an enumerated type is a list of named values in a fixed order, and a pointer holds the memory address of a value of one stated type.

Composite types are built from other types. A record groups fields of different types, a set holds values with no order and no repeats, and a class holds properties together with methods.

Rules to remember

Common mistake

Students write an enumerated value in quotation marks, as in Day ← "Tue". The values are identifiers, so the correct form is Day ← Tue, and a name that is not in the list is not allowed.

Worked example

TIntPtr is a pointer type to INTEGER.
DECLARE A, B : INTEGER
DECLARE P : TIntPtr
A ← 10
B ← 4
P ← ^A
P^ ← P^ − 3
P ← ^B
P^ ← P^ + A
What are the final values of A and B?

  1. P ← ^A makes P point to A, so P^ ← P^ − 3 changes A: 10 − 3 = 7.
  2. P ← ^B makes P point to B; A is not changed by this.
  3. P^ ← P^ + A changes B: 4 + 7 = 11.

Answer: A = 7 and B = 11.

Ask about this

Type what you are stuck on. The helper finds the explanation, worked examples and the notes to read, free and instant.

Keep going

File organisation and access →Take a Computer Science A2 test