Maxloc and minloc intrinsics should return a scalar when the array has rank of 1 and DIM=1 is used. In the following code, the call at (A) returns an integer scalar which is assigned to the value of max_index. At (B) the value of the array element max_index is printed out.
However, when the two operations are combined at (C) (using maxloc to get the index but without using an intermediate variable) I get a compiler error. The compiler thinks the RHS is an array.
I know this seems a rather convoluted way to get the maximum (I could just use maxval), but actually in my application I am using maxloc and minloc to get indices of doubly indexed array sections like.
ind = ix(maxloc(x(ix(lower:upper)),1)+lower-1)
which exhibits the same bug but is harder to follow.
The example below is simpler.
program maxim
implicit none
integer :: max_index
real :: x(5), max_val
x = (/1.0, 2.0, 5.0, 4.0, 3.0/)
! (A) Maximum index should be 3
max_index = maxloc(x(1:5), 1)
print *,'Maximum is at location ', max_index, ' (should be 3)'
! (B) Max value should be 5.0
print *, 'Maximum value is ', x(max_index), ' (should be 5.0)'
! (C) This line should compile should also give 5.0, but the compiler
! thinks that the RHS is an array and issues an error 945.
!*****************************
max_val = x(maxloc(x(1:5), 1))
!*****************************
print *, 'Maximum value is ', max_val, ' (should be 5.0)'
end program maxim