slightly OT: nullifying an array of pointers

Paul Thomas paulthomas2@wanadoo.fr
Thu Jul 6 14:26:00 GMT 2006


Daniel,

>
>
>  array%i = null()         ! <-- invalid, NULLIFY(array%i) does not 
> work either
> END PROGRAM
>
> The workaround using an explicit DO-loop is  obvious, but could
> someone explain why it is necessary in this case?

It does not comply with a constraint in 6.1.2 of the F95 standard:

6.1.2 Structure components

A structure component is part of an object of derived type; it may be 
referenced by a subobject
designator. A structure component may be a scalar or an array.

R612 data-ref is part-ref [ % part-ref ] ...
R613 part-ref is part-name [ ( section-subscript-list ) ]

Constraint: In a data-ref, each part-name except the rightmost shall be 
of derived type.

Constraint: In a data-ref, each part-name except the leftmost shall be 
the name of a component of
the derived-type definition of the type of the preceding part-name.

Constraint: In a part-ref containing a section-subscript-list, the 
number of section-subscripts shall
equal the rank of part-name.

The rank of a part-ref of the form part-name is the rank of part-name. 
The rank of a part-ref that has
a section subscript list is the number of subscript triplets and vector 
subscripts in the list.

Constraint: In a data-ref, there shall not be more than one part-ref 
with nonzero rank. A part-name
to the right of a part-ref with nonzero rank shall not have the POINTER 
attribute.
The rank of a data-ref is the rank of the part-ref with nonzero rank, if 
any; otherwise, the rank is
zero. The parent object of a data-ref is the data object whose name is 
the leftmost part name.

This last one is what does you in.

However, you can achieve what you want with an derived type constructor:

PROGRAM initialize_ptrarray
 INTEGER, PARAMETER :: n = 100

 TYPE :: intptr
   real :: x
   INTEGER, POINTER :: i
   character(20) :: chr
 END TYPE
 TYPE(intptr), DIMENSION(n) :: array

 array = intptr (2.0_4, null(), "an arbitrary string")
END PROGRAM

Paul






More information about the Fortran mailing list