• Keine Ergebnisse gefunden

CHARACTER TYPE

Im Dokument COMPUTER SYSTEMS (Seite 102-107)

2. PROGRAM STRUCTURE

3.8 CHARACTER TYPE

A character value or character string consists of one or more 8-bit

ASCII characters (listed in appendix A). A character constant is written as a sequence of characters preceded and followed by a delimiter, which can be an apostrophe or quotation mark. The delimiter can appear as a character within the string if it appears twice in succession; the double character is interpreted as a single character. Blanks in a character string are significant.

Examples:

Constant 'ABC' or "ABC" is stored internally as ABC.

Constant' ,., or ... is stored internally as Constant , .... , is stored internally as '"'

The length of a character constant is the number of characters between its delimiters, with each pair of consecutive delimiters counted as a single character. A character variable is declared with its length by the CHARACTER type statement, before any executable statements. The length of each variable is specified by an integer following an asterisk;

this specifier can follow any individual variable name or can follow the word CHARACTER for all names lacking length specifiers. The following example shows a declaration for variables X and Z of length 5, W of length 2, and Y of length 7.

CHARACTER*5 W*2,X,Y*7,Z

User-specified functions of type character must be declared in the same manner as character variables. Actual and dummy arguments of type character must agree in length.

The length of a character constant must be greater than 0 and less than 1317; this limitation derives from the number of lines allowed in a FORTRAN statement. The length of a character value must be greater than

o and less than 16393 or 2 14 _1.

Each character within a string has a position that is numbered ordinally from the first character. These positions are used in specifying

character substrings, discussed in subsection 3.8.2, Character Substrings.

When all characters of a character entity become defined, the character entity becomes defined. See subsection 4.5.3, Definition.

The ANSI FORTRAN Standard does not provide for the use of quotation marks as delimiters, and does not specify a maximum length for a character value.

3.8.1 CHARACTER TYPE STATEMENT

The CHARACTER statement declares a symbolic name to be of type character and shows the length of each character entity declared.

CHARACTER [*len[,]]nam[*len][,nam[*len]] ...

len Length specification (number of characters) for an entity

nam Variable name, symbolic name of a constant, function name, dummy procedure name, or array name or array declarator The length specification following the word CHARACTER refers to each entity without a length specification. If the CHARACTER type statement does not include a length specification, the length is assumed to be one.

The length specification, len, can be an unsigned, nonzero integer constant or a positive, nonzero integer constant expression enclosed in parentheses. The value of len cannot be greater than 214_ 1.

The ANSI FORTRAN Standard does not specify a maximum character length.

If the entity is an external function, a dummy argument, or a character constant, len can be specified as an asterisk enclosed in parentheses:

for example, CHARACTER*(*). If the entity is an external function, the

function name must appear in a FUNCTION or an ENTRY statement in the same subprogram, and the length is obtained from the referencing program

unit. If the entity is a dummy argument, the dummy argument assumes the length of the associated actual argument. If the entity is a character constant with a symbolic name, the constant will assume the length of its corresponding constant expression defined later in a PARAMETER statement.

3.8.2 CHARACTER SUBSTRINGS

A character substring consists of one or more contiguous characters

within a character variable or character array element (see later in this section under the heading Arrays). A substring name takes the following form:

cvname ([begpos]:[endpos])

cvname Name of a character variable or character array element.

([begpos]:[endpos])

Substring designator. begpos and endpos are integer

expressions designating the beginning and ending character positions of the substring. The minimum and default value of begpos is 1; the maximum and default value of endpos is the last position.

Examples:

Substring: Designates Characters:

STRINGA(6:9) 6 through 9 of variable STRINGA STRINGB(4: ) 4 through last of variable STRINGB

STRINGC(2,6)(1:3) 1 though 3 of array element STRINGC(2,6) STRINGD(5,4)( :7) 1 through 7 of array element STRINGD(5,4)

3.8.3 ARGUMENTS OF TYPE CHARACTER

Arguments of type character can be character variables, substrings, or, in external procedures, character arrays. An actual argument associated with a character dummy argument must be of type character, with a length exceeding or equaling that of the dummy argument.

If a function subprogram name is of type character, each entry name in the function subprogram must be of type character; the function name and all entry names must have the same declared length, whether it is an integer or (*), denoting adjustable length. A character dummy argument whose length is specified as (*) must not appear as an operand for concatenation, except in a character assignment statement.

If the length len of a dummy argument of type character is less than the length of an associated actual argument, the leftmost len

characters of the actual argument are associated with the dummy argument;

that is, the rightmost characters are truncated. len must not exceed the actual argument's length (this can cause unpredictable execution-time errors).

If a dummy argument of type character for an external procedure is an array name, the restriction on the array's length (described in the previous paragraph) is for the entire array and not for each array

element. The length of a dummy array element can differ from the length of an associated actual array, element, or element substring; but the dummy array must not extend beyond the end of the associated actual array.

When an actual argument is a character substring, the argument's length is the substring's length. If an actual argument is the concatenation of two or more operands, the argument's length is the sum of the operands' lengths. A character expression involving concatenation of an operand with a length specification of (*) cannot be used as an actual or dummy

argument unless the operand is the name of a constant in a character assignment statement.

Substring expressions are evaluated immediately preceding argument association; the expression values remain constant as long as argument association continues.

3.9 POINTERS

A pointer is a type INTEGER variable whose value is used as the address of another entity, which is called a pointee. A pointer is declared by the POINTER statement, which also specifies its pointee.

Example:

POINTER (P,B),(Q,C)

The above statement declares pointer P and its pointee B, and pointer Q and pointee C; the pointer's current value is used as the pointee's address whenever the pointee is referenced.

The pointee does not have an address until the pointer's value is defined: the pointee's value starts at the location specified by the pointer. A pointer cannot be assigned a literal constant number; its value must derive from the location of an entity or be defined in terms of other pointers:

• The LOC function returns the address of a variable. Example:

P=LOC(X)

• A pointer can be defined in terms of another pointer. Example:

Q=P+100

The CFT compiler assumes that a pointee's storage is never overlaid on another variable's storage; that is, a pointee should never be associated with another variable or array. The programmer is responsible for

preventing such association. In the following example, Band C have the same pointer, so the result is likely to be invalid.

POINTER (P,B),(P,C) REAL X,B,C

P=LOC(X) B=1.0 C=2.0 PRINT *,B

3.9.1 POINTER STATEMENT (CFT EXTENSION)

The P0INTER statement declares one variable to be a pointer (which is of type pointer), and another variable to be its pointee; that is, the pointer's value is the address of the pointee.

POINTER (p,a)[,(p,a)] •.•

p

a

Pointer to the corresponding a. p contains the word address of the location of a.

Variable pointed to by p. The content of p is used for any reference to a; therefore a is not assigned

storage. a cannot be a dummy argument nor appear in a COMMON, EQUIVALENCE, or DATA statement. a cannot be of type CHARACTER.

ai can be dimensioned in a separate type or DIMENSION statement or dimensioned in the pointer list itself, as in the following example:

POINTER (IX,X(N,O:M»

In a subroutine or function, the a dimension expression can contain references to variables in a common block or to dummy arguments.

The pointer, p, is a simple variable of type INTEGER and can appear in a common list or be a dummy argument in a subprogram. p can be set with a LOC function reference or as an absolute address, as in the following example:

COMMON POOL (100000) INTEGER JCB (128), WORD64 REAL A (l),B(l),C(l) POINTER

(PJCB,JCB),(IA,A),(IB,B),(IC,C),(ADDRESS,WORD64) DATA ADDRESS/64/ PJCB

=

0 IA

=

LOC(POOL)

IB

=

IA + 1000 IC

=

IB + N

In effect, WORD64 refers to the contents of absolute address 64; JCB is an array occupying the first 128 words of memory; A is an array of length 1000 located in blank common; B follows A and is of length N;

C follows B. A, B, and C are equated to POOL and possibly to each other, depending on the subscript usage. Similarly, WORD64 is the same as

JCB(65). However, CFT makes no checks for possible equivalence overlap.

Each a is assumed to be a distinct entity.

Any change in the value of a pointer causes subsequent references to the corresponding pointee to refer to the new location.

Besides providing a limited form of dynamic storage allocation, the POINTER statement can manipulate linked lists, as in the following example:

SUBROUTINE FINDSAM (SAMSSPOT) POINTER (SAMSSPOT, RECORD (N»

COMMON N INTEGER RECORD

10 IF (RECORD (4) .EQ.'SAM') RETURN SAMSSPOT

=

RECORD (25)

IF (SAMSSPOT .NE.O) GO TO 10 PRINT 20

20 FORMAT ("SAM'S NOT HERE")

Im Dokument COMPUTER SYSTEMS (Seite 102-107)