Search code examples
fortranfortran77fortran-common-block

Char array dimensioning confusion in Fortran 77


I have the following piece of code in my subroutine:

character    x*256 ,y*80
common /foo/ x     ,y(999)

Well, I did not actually write this. So I don't understant the dimensions here. Is y an 999 element wide array of 80 character long strings?

If so, how can I define this properly in Fortran 90, without the common block?


Solution

  • I will first say that the code you have is "proper" Fortran 90, but I agree with wanting to move away from common blocks.

    There is, essentially, nothing specific to the character nature of the declaration. Whenever

    <type> A
    common /foo/ A(<size>)
    

    is used there are two parts to the declaration of A, as well as the common association: the type and the dimension. Ignoring the association, declaration of the dimension in the common statement is allowed and the above is like

    <type> A
    dimension A(<size>)
    

    This is in turn the same as

    <type>, dimension(<size>) :: A
    

    Coming to the specific example, the type is a character of length 80. Your non-common declaration would simply be

    character(len=80), dimension(999) :: y
    

    Indeed, then, y is a rank-1 array of size 999 of length-80 characters. y(10) is a scalar length-80 character (the 10th element of the array y).

    x(10) isn't correct syntax, as the (10) is array indexing, and x is a scalar. For substrings a different indexing is required. x(10:10) is the 10th character of the character variable x; y(10)(10:10) is the 10th character of the 10th element of the character array y.