Arrays in Pascal allow the definition of variables that can contain multiple data items of the same type, but record types in Pascal are user-defined data types available in Pascal, allowing you to combine data of different kinds.
Records in Pascal consist of various fields. For instance, if you want to track the books available in a library, you can track the following attributes of each book, including:
- Book Title
- Author of the Book
- Book Classification
- Book ID
Defining Records in Pascal
To define a record in Pascal, you can use a type declaration. The record type in Pascal is defined as follows:
Below is how to declare a record type Book:
General structure for defining variables of record type in Pascal:
Declaration
r1, r2, ... : record-name;
Alternatively, you can directly define variables of record type in Pascal:
Accessing Fields in Pascal Record Type
To access any fields within a record type in Pascal, we can utilize the access operator (.). The member access operator is represented by a dot between the record variable name and the field you wish to access.
Below is an example illustrating how to access fields within a Pascal record type:
When the code above is compiled and executed, it will return the following result:
Passing a Pascal Record as an Argument to a Subprogram
You can pass a Pascal record as an argument to a subprogram just like you can pass any variable or pointer.
In the example below, you can access record fields similarly to how you access them in the previous example:
When the code above is compiled and executed, it will return the following result:
Pointers in Pascal Record
You can define a pointer to a record similar to how you define pointers to any variables as shown below:
Now you can store the address of a record variable in a pointer variable defined above. To declare a pointer variable, you use the var keyword:
var
r1, r2, ... : record-ptr;
Before using these pointers, you must allocate memory for a record variable, which will be manipulated by these pointers.
new(r1);
new(r2);
To access fields of a record using a pointer to that record, you must use the ^ operator as shown below:
Finally, deallocate the memory used in case the memory is no longer needed:
dispose(r1);
dispose(r2);
The example below uses a pointer to the Book record:
When the code above is compiled and executed, it will return the following result:
With Statement in Pascal Records
As mentioned above, we can access the fields of a record by using the dot operator (.). By this method, the record variable name has to be written multiple times. Therefore, using the With statement is an alternative solution in this case.
Below is an example piece of code taken from the first example:
A similar code snippet can be written using the With statement:
So, the article on Mytour has just provided you with information about records in Pascal. To delve deeper into operators in Pascal, the general structure of a Pascal program, readers can refer to some previous articles on Mytour.
In addition, you can explore more articles about Variant Variables in Pascal here.
