2

I am trying to have an allocatable character inside a common block, in Fortran 77. But the compiler always complains

Error: COMMON attribute conflicts with ALLOCATABLE attribute at (1)

The code I had is

      subroutine word()
          character (len = :), allocatable :: a
          common /a/ a
          a = 'word'
      end subroutine word

      program main
          character (len = :), allocatable :: a
          common /a/ a
          call word()
          print *, a
      end program main

The only way that I saw to make it work is not to use allocatable

      subroutine word()
          character * 4 :: a
          common /a/ a
          a = 'word'
      end subroutine word

      program main
          character * 4 :: a
          common /a/ a
          call word()
          print *, a
      end program main

But this would be cumbersome because I would have to know the length of a character in advance.

How could I make allocatable to work in a common block?

zyy
  • 1,271
  • 15
  • 25
  • 1
    The linked question asks about allocatable arrays, but the question and answer hold for all allocatable objects. – francescalus Mar 11 '19 at 08:44
  • 2
    You couldn't have allocatable anything in Fortran 77 - these new fangled things were only introduced in 1990. So as you are not using Fortran 77 but something more modern please don't use common which are evil, use modules instead – Ian Bush Mar 11 '19 at 09:32
  • @IanBush Thanks, I just have to work in Fortran 77 with `common` in some historic code. – zyy Mar 11 '19 at 15:21

0 Answers0