Lecture 18: Records |
The three file cabinets store things of the
|
The file cabinet in the center is used for
|
A record is a set of variables of mixed type. |
|
A visualization of a record, namely a motley set of variables. Each variable inside a record is called a field. Here we have 5 fields: a byte (b), a real (f), a boolean (c), a single array of reals (r) and a double array of reals (m). |
To declare a record we do the following
Var name:
record item1: type1; item2: type2; | itemN: typeN; end; |
As an example, the definition of a record containing the information of a student might have fields for name, year, and tuition fees paid:
Var student:
record name: string; year: integer; propinas: boolean; end; |
Var students: array[1..1000] of
record name: string; year: integer; propinas: boolean; end; |
Later we will see that we can much more easily do this with a new type definition (see lecture 19).
name.field |
For example, to assign values to the record student we can do the following
student.name := 'Peter Stallinga';
student.year := 2002;
student.propinas := TRUE;
or in the example of the array of records students:
i := 1055;
students[i].name := 'Peter Stallinga';
students[i].year := 2002;
students[i].propinas := TRUE;
Note the structure of arrays of records. students
is an array of records, therefore, students[i]
is one of these records and if we want to assign something to a field we
use the period and the fieldname, so students[i].name
is a string containing the name of student number i.
Wrong syntax would be students.name[i]
(we could use this if we had a single record students
containing a field name that is an
array)
Also wrong: students.[i]name,
which doesn't make sense at all.
Another example:
Var coordinate:
record
x: real;
y: real;
end;
coordinate.x := 1.0;
coordinate.y := 0.0;
This is not exactly a set of variables of mixed type, so in principle we could also do this with an array:
Var coordinate: array[1..2] of real;
coordinate[1] := 1.0;
coordinate[2] := 0.0;
but, the first version, with the record is more logical.
Another example:
Var address:
record
street: string;
housenumber:
integer;
andar: integer;
porta: char;
end;
address.street := 'Rua Santo Antonio';
address.housenumber := 34;
address.andar := 3;
address.porta := 'E';
writeln(address.street, ' ',
address.housenumber);
writeln(address.andar, address.porta);
In the above example we had to write many time the word "address". To save time, in PASCAL exists the combination with recordname do, which means that in the instruction after it (everything between begin and end) the variables (that the compiler doesn't know otherwise!) get the recordname in front of it. Therefore, the above two lines of code can be rewritten in a more readable form as
with address do
begin
writeln(street,
' ', housenumber);
writeln(andar,
porta);
end;