SHAPE

Inquiry Intrinsic Function (Generic): Returns the shape of an array or scalar argument.

Syntax

result = SHAPE (source [, kind])

source

(Input) Is a scalar or array. It may be of any data type. It must not be an assumed-size array, a disassociated pointer, or an allocatable array that is not allocated.

kind

(Input; optional) Must be a scalar integer initialization expression.

Results

The result is a rank-one integer array whose size is equal to the rank of source. If kind is present, the kind parameter of the result is that specified by kind; otherwise, the kind parameter of the result is that of default integer. If the processor cannot represent the result value in the kind of the result, the result is undefined.

The value of the result is the shape of source.

The setting of compiler options specifying integer size can affect this function.

Example

SHAPE (2) has the value of a rank-one array of size zero.

If B is declared as B(2:4, -3:1), then SHAPE (B) has the value (3, 5).

The following shows another example:

INTEGER VEC(2)

REAL array(3:10, -1:3)

VEC = SHAPE(array)

WRITE(*,*) VEC ! prints 8 5

END

!

! Check if a mask is conformal with an array

REAL, ALLOCATABLE :: A(:,:,:)

LOGICAL, ALLOCATABLE :: MASK(:,:,:)

INTEGER B(3), C(3)

LOGICAL conform

ALLOCATE (A(5, 4, 3))

ALLOCATE (MASK(3, 4, 5))

! Check if MASK and A allocated. If they are, check

! that they have the same shape (conform).

IF(ALLOCATED(A) .AND. ALLOCATED(MASK)) THEN

B = SHAPE(A); C = SHAPE(MASK)

IF ((B(1) .EQ. C(1)) .AND. (B(2) .EQ. C(2)) &

.AND. (B(3) .EQ. C(3))) THEN

conform = .TRUE.

ELSE

conform = .FALSE.

END IF

END IF

WRITE(*,*) conform ! prints F

END

See Also