Beliavsky / FortranTip

Short instructional Fortran codes associated with Twitter @FortranTip
https://zmoon.github.io/FortranTipBrowser/
The Unlicense
64 stars 14 forks source link

Tip: many-one vector subscripts can be used but not on the LHS #37

Open Beliavsky opened 2 years ago

Beliavsky commented 2 years ago

A vector subscript can have repeated elements, creating a many-one section, for example

x([1,2,1])

Such a section must not appear on the left hand side of an assignment, because of the ambiguity, but some compilers do not catch this.

program many_one
implicit none
integer, parameter :: ncou = 4
character (len=6)  :: continents(2) = ["Asia  ","Europe"]
character (len=6)  :: countries(ncou) = &
   ["France","Japan ","China ","Italy "]
integer            :: ic(ncou) = [2, 1, 1, 2]
character (len=*), parameter :: fmt = "(*(a10))"
print fmt, "country",countries
print fmt, "continent",continents(ic) 
! line above is a valid many-one section
! line below is invalid since many-one array section
! must not appear on left hand side
continents(ic) = ["a","b","c","d"]
print fmt, continents
end program many_one
! gfortran and Intel Fortran output is below.
! They do not catch the invalid many-one assignment.
!
!    country    France    Japan     China     Italy 
!  continent    Europe    Asia      Asia      Europe
!    c         d