# 4.5 Records and structures

If you want to use structures (or records as they are called in Pascal) you will find there is no native support for that in 4tH. Relax, there is no such support in Forth either, except for some proprietary code. That doesn't mean you can't do it. In order to get you on your way we'll present some examples here.

The easiest way is to allocate a structure in the Character Segment. You can make use of the fact that subsequently defined strings will form one single contiguous chunk of memory. The first thing we'll do is define the length of the fields (or strings).

         \ length of fields
         32 constant #Name
         64 constant #Address
         32 constant #City
         12 constant #Age

Of course, you don't have to, but it will make it a lot easier to maintain your program. Then we define the fields:

         \ define fields
         #Name      string Person.Name
         #Address   string Person.Address
         #City      string Person.City
         #Age       string Person.Age

This might be a familiar example to you. We'll store information on a single person in this structure. Now we got the fields, the length of the fields, but still no way to manipulate the complete structure. In order to do that we'll define some more variables:

         \ define record
         Person.Name Constant Person
         #Name #Address #City #Age + + + Value #Person

This will create a pointer which points to the first field, which is also the start of the structure. Then we define a value which hold the length of all fields added together. Now we create some utilities to manipulate the structure and its fields:

         \ helper utilities
         : into copy drop ;         ( s a -- )

So we can write routines like this:

         : EraseRecord
               Person #Person blank      \ erase buffer
         ;

Which wipes a structure (fills it with blanks) or initializes the fields:

         : InitRecord                    \ initialize fields
               " Hans Bezemer" Person.Name    into
               " Turfmarkt 97" Person.Address into
               " Den Haag" Person.City        into
               " 36" Person.Age               into
         ;

Of course, you can also use 'ACCEPT' to enter the contents of the fields. Fields act like ordinary strings. Note that numbers are stored as strings as well. This is not too much of a problem since 'NUMBER' can convert them back to numbers anyway.

This is a very simple use of structures. If you want to have multiple occurences of the same structure, that can be done as well. But it is a little more complicated to implement. We leave that to you to figure out.