Previous | Table of Contents | Next |
5.2.4.4. Strings
A special array defined in Turbo Pascal to handle character data is referred to as a string. There are two types of strings available to work with in Turbo Pascal 7: Pascal-type strings and null-terminated (or C-type) strings.
Pascal-type strings are specially defined in Turbo Pascal with the string identifier. Without any qualifiers, this represents an array[0..255] of char implicitly. The maximum length of a string type variable is 255 characters in this array, as it is stored in memory that way. The 0th position in this array holds an ASCII character equivalent in value to the dynamic (current) length of the string.
For example, if the phrase New York were placed in a string-type variable named city, the variable would use 256 bytes of memory, with data occupying 9 bytes of that space. Figure 5.2 shows the 9 bytes in this 256-byte array.
Figure 5.2. An illustration of storage of a variable type: string.
What if you want to save all the excess space by defining a string variable as type string if you know that a string that long will never be assigned to that variable? If the range of possible string variables that will be stored is known, such as the case in which a program might be written involving the storage of month names, then the string variable can be further defined to have a smaller limit. That is done by placing brackets with the maximum amount inside them after the string keyword. For example, with the months, we could define a variable in the var section like this:
month: string[15];
In this case, month occupies 16 bytes of memory and holds a maximum string of 15 characters.
Another case of a string that can be supported in Turbo Pascal is the null-terminated string. This type is, as the name implies, an array of characters, starting with the text itself and ending with a null character, or #0. These types are either supportable by Turbo Pascal or can be supported by written code; though, in pure Turbo Pascal coding, the Pascal-type string is more commonly used.
5.2.4.5. Constant and Data Type Definitions
Turbo Pascal supports definition of constant values as well as data type definitions. Constant values are used for various purposes to aid in updating a program. Denoted by variables that appear under a header const, a constant type variable should be used throughout a program for values that do not change rapidly over short periods of time, such as month names and tax rates. A constant variable generally should not be modified by code in the program.
For example, in Listing 5.14, a constant variable could be placed at the top with a variable identity and have it used to change the 3s in the control statements of the program to that constant variable, as well as the 3s in the array (a constant variable may be used for arrays because the value is specified at compile time). This would result in a very easy modification to change the 3 × 3 nature of the matrix evaluation to an N × N evaluation of any reasonable value for N.
Data type definitions are used to help in modularity in certain ways, and are necessary to help define other data types that are described later. They are contained under a header denoted as type. When a type definition is made, generally variables that use this type definition exist in the var section or elsewhere. In essence, you are defining a new valid data type. Demonstrations of both constant variables and type data declarations are shown in Listing 5.15.
Listing 5.15. A demonstration of constants and data type declarations.
program fig15; { constants and data types demonstration } const months: array[1..12] of string[3] = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov, Dec); { shows how to define a constant array, otherwise something like taxrate = 0.15; {will be sufficient to specify one constant variable } type monthtype = string[3]; var answer: monthtype; month: byte; begin write(What number of month: ); readln(month); answer := months[month]; writeln(answer); end.
5.2.4.6. Records
The data type used to group unrelated items in Turbo Pascal is called a record. This is not an intrinsic data type definition; hence, one must be defined in the type section. Any kind of variable, including another record type, may be defined within a record. A record data type might be used in the process of storing data for a phonebook program. A record definition that might be used in the type section of such a program might appear like this:
phonerecord = record firstname: string[15]; lastname: string[10]; midinit: char; areacode, exchange, number: integer; city: string[20]; end;
There are two possible ways to access such a record structure when defined as a variable in a program. One is to specify the name of the variable, followed by a dot, and then the subidentifier as defined in the type declaration. The other is to encompass the code dealing with the particular record type in a with-do construct. There is no technical difference in either method other than the reduced typing the second method can produce. A diagram of memory storage of this construct is shown in Figure 5.3, and a demonstration of defining and using records is shown in Listing 5.16.
Figure 5.3. An illustration of a record in memory, using the preceding sample.
Previous | Table of Contents | Next |