Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

About the lbound intrinsic function

John4
Valued Contributor I
684 Views
Hi,

This is just probably a silly question, since I already know what solves the problem, but... The issue might be better explained by the following code:

!======================================
module mod1
type mytype
integer :: a
end type mytype
end module mod1

program Console1

use ifcore
use mod1

implicit none

integer, parameter :: n = 3
character :: key
type(mytype), allocatable :: var1(:,:)

allocate(var1(-1:n+2, -1:n+2))

write(*,*) "the lower bound for var1 is ",lbound(var1, 1)
write(*,*) "the lower bound for var1%a is ",lbound(var1%a, 1)
write(*,*) "the lower bound for var1(:,:)%a is ",lbound(var1(:,:)%a, 1)

key = getcharqq()

end program Console1
!======================================

Why does the lbound function return the correct lower-bound only when the component "%a" is not present? For the other cases it always returns "1".
0 Kudos
2 Replies
Steven_L_Intel1
Employee
684 Views

Because the Fortran standard says so! You are by no means the first person to be confused by this.

The standard says:

7

Result Value.
8 Case (i): If ARRAY is a whole array or array structure component and either ARRAY is an
9 assumed-size array of rank DIM or dimension DIM of ARRAY has nonzero extent,
10 LBOUND (ARRAY, DIM) has a value equal to the lower bound for subscript DIM
11 of ARRAY. Otherwise the result value is 1.

12
Case (ii): LBOUND (ARRAY) has a value whose ith component is equal to LBOUND (AR-
13 RAY, i), for i = 1, 2, ..., n, where n is the rank of ARRAY.

Var1%a is neither a whole array nor an array structure component (it is a structure component of an array) and thus is a "new variable" that has the correct number of elements as in the parent array but does not inherit its lower and upper bounds.

0 Kudos
John4
Valued Contributor I
684 Views
I always thought that the rule only applied to referenced elements (i.e., either pointers or arguments), so it seems I have to re-read the F95 standard before the F03 one gets to be implemented in full.

Thanks for the reply!
0 Kudos
Reply