Chapter 6. Using Data

Table of Contents
6.1. Constants and Variables
6.2. Substrings
6.3. Structure Components
6.4. Arrays
6.5. Pointers and Allocatable Objects

Chapter 5, explains how data objects are created and how their attributes are specified. This chapter explains how these objects can be used. The appearance of the name or designator where its value is required is a reference to the object. When an object is referenced, it must be defined; that is, it must have a value. The reference makes use of the value. Consider the following two statements:

A = 1.0
B = A + 4.0

In the first statement, the constant value 1.0 is assigned to the variable A. It does not matter whether A was previously defined with a value or not; it now has a value and can be referenced in an executable statement. In the second statement, A is referenced; its value is obtained and added to the constant 4.0 to obtain a value that is then assigned to the variable B. The appearances of A in the first statement and B in the second statement are not considered to be references because their values are not required. The appearance of A in the second statement is a reference.

A data object can be a constant or a variable. Variables and constants can be scalar objects (with a single value) or arrays (with any number of values, all of the same type). Strictly speaking, there is no such thing as an array constant. An array constructor made up of all constant values is a constant expression, not an array constant. Also note that a derived type is a scalar value. Similar to an array constructor, a derived type constructor composed of all constant values is a constant expression, not a structure constant.

Arrays are said to be dynamic if their size can change. Automatic arrays are discussed in Section 5.8; they are created on entry to a procedure, and their sizes are determined at that time. Allocatable arrays or pointer arrays can change size as well. The declared rank cannot change, but the extents of the dimensions may change with each reallocation or pointer assignment.

If a variable or constant is a portion of another object, it is called a subobject. A subobject can be one of the following items:

A variable is referenced by its name, whereas a subobject is referenced by a designator. A designator indicates the portion of an object that is being referenced. Each subobject is considered to have a parent and is a portion of the parent. Each of the subobjects is described in this chapter.

This chapter also explains how to create and release pointers and allocatable arrays by using the ALLOCATE and DEALLOCATE statements. In addition, you can disassociate pointers from any target object by using the NULLIFY statement.

A reference to a variable or subobject is called a data reference. Guidelines exist for determining whether a particular data reference is classified as a character string, character substring, structure component, array, array element, or array section. These classifications are perhaps of more interest to compiler writers than to users of the language, but knowing how a data reference is classified makes it clearer which rules and restrictions apply to the reference, and easier to understand some of the explanations for the formation of expressions. Briefly, character strings and substrings must be of type character. Arrays have the DIMENSION attribute. Some data references can be classified as both structure components and array sections. In general, if a data reference contains a percent sign (%), it is a structure component, but its actual classification can be determined by other factors such as a section subscript or the rightmost element of the reference. If a substring range appears in a data reference, it must appear at the right end of the reference; the reference is considered to be a substring unless some component of the reference is an array section, in which case the data reference is considered to be an array section that just happens to have elements that are substrings. For a component reference to be classified as an array element, every component must have rank zero and a subscript list must appear at the right end of the reference. Section 6.1, through Section 6.4.5, contain many examples that demonstrate how these guidelines for classification apply.

6.1. Constants and Variables

A constant has a value that cannot change; it can be a literal constant or a named constant (parameter). As explained in Chapter 4, each of the intrinsic types has a form that specifies the type, type parameters, and value of a literal constant of the type. For user-defined types, there is a structure constructor to specify values of the type. If all of the components of a structure constructor are constants, the resulting derived-type or type-alias value is a constant expression. Array constructors are used to form array values of any intrinsic or user-defined type. If all array elements are constant values, the resulting array is a constant array expression. A reference to a constant is always permitted.

A variable has a name such as A or a designator such as B(I), and may or may not have a value. If it does not have a value, it must not be referenced. Variables are defined as follows:

Table 6-1.

 

variable

is

scalar_variable_name

 

 

or

array_variable_name

 

 

or

subobject

 

subobject

is

array_element

 

 

or

array_section

 

 

or

structure_component

 

 

or

substring

 

logical_variable

is

variable

 

default_logical_variable

is

variable

 

char_variable

is

variable

 

default_char_variable

is

variable

 

int_variable

is

variable

 

default_int_variable

is

variable

A logical_variable must be of type logical, and a default_logical_variable must be of type default logical. A char_variable must be of type character and a default_char_variable must be of type default character. The Cray Fortran Compiler does not support any nondefault character types. An int_variable must be of type integer and a default_int_variable must be of type default integer.

Variables can be of any type, except for Boolean (or typeless). There are contexts in which a variable must be of a certain type. In some of these cases, terms, such as logical_variable, character_variable, or Cray pointer, provide precise limitations.

Note: The Fortran standard does not specify Boolean (or typeless) constants or Cray pointers.

A subobject with a constant parent is not a variable.

A single object of any of the intrinsic or user-defined types is a scalar. A set of scalar objects, all of the same type and type parameters, can be arranged in a pattern involving columns, rows, planes, and higher-dimensioned configurations to form an array. An array has a rank between one and seven. A scalar has rank zero. In simple terms, an array is an object with the DIMENSION attribute; a scalar is not an array. For example:

TYPE PERSON
   INTEGER AGE
   CHARACTER(LEN = 40) NAME
END TYPE PERSON

TYPE(PERSON) FIRECHIEF, FIREMEN(50)
CHARACTER*(20) DISTRICT, STATIONS(10)

The following data references are classified as indicated by the comments on each line:

DISTRICT      ! character string
DISTRICT(1:6) ! substring
FIRECHIEF%AGE ! structure component
FIREMEN%AGE   ! array of integers
STATIONS      ! array of character strings
STATIONS(1)   ! array element (character string)
STATIONS(1:4) ! array section of character strings

The following code segment shows that a subobject can have a constant parent:

CHARACTER(*), PARAMETER :: MY_DISTRICT = "DISTRICT 13"
CHARACTER(2) DISTRICT_NUMBER
DISTRICT_NUMBER = MY_DISTRICT(10:11)

DISTRICT_NUMBER has the value 13.