Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor III
16 Views

character array allocation, alternative definition fails

Hi all,

here the basics of allocated characters are discussed:

https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/287349

Now I have the following minimal working example:

!
program character_array_allocation
  implicit none
  ! Variables
  character(len=:),dimension(:), allocatable :: foo
  
  allocate(character(len=10),dimension(20) :: foo) ! this fails
! 1>------ Build started: Project: character_array_allocation, Configuration: Debug Win32 ------
! 1>Compiling with Intel(R) Visual Fortran Compiler 17.0.1.143 [IA-32]...
! 1>character_array_allocation.f90
! 1>... \character_array_allocation.f90(7): error #5082: Syntax error, found ',' when expecting one of: . , [ % . ( :: )
! 1>compilation aborted for ... \character_array_allocation.f90 (code 1)
  
  
  !allocate(character(len=10) :: foo(20)) ! this works
  

end program character_array_allocation

Why causes the alternative format in line 7 a compiler error? The version in line 15 works fine.

Thanks, Johannes

0 Kudos
8 Replies
Highlighted
Valued Contributor II
16 Views

I do not think the syntax that is failing is valid Fortran. At the very least I do not remember seeing that.

0 Kudos
Highlighted
New Contributor III
16 Views

Hi Arjen,

thanks for your answer. My thought was, if the definition in line 5 is valid, why should it not be valid in the allocation. Sounded logically for me, but maybe isn't. gfortran (4.8.x) does not accept line 7 also.

0 Kudos
Highlighted
Valued Contributor II
16 Views

Yes, that might have been logical, indeed. I do not know why it should not be, but the type clause "character(len=10):" is a later addition to the allocate statement. (It is necessary because of polymorphic variables. And its use for specifying the length is a bonus.)

0 Kudos
Highlighted
Valued Contributor III
16 Views

I also think your syntax is invalid.

As a side note I find the "dimension" attribute a bit too verbose and rather unnecessary in the language. In my mind it is a throw back to ancient history. The only benefit I can think of is where you have a long list of variables that all have the same dimensions.

 

 

 

 

0 Kudos
Highlighted
New Contributor III
16 Views

Thanks all,

the dimension attribute seems not to be allowed at all in an allocation statement. It does not work for real kinds, too. I agree, that this seems to be wrong syntax and the compiler correctly gives an error.

The dimension attribute is used widely in text books and I personally like it, because it make the code better readable IMHO. But that's a personal preference...

0 Kudos
Highlighted
Black Belt Retired Employee
16 Views

DIMENSION is for declarations - it's fine there, though is generally considered old-fashioned. It's not for ALLOCATE.

0 Kudos
Highlighted
New Contributor III
16 Views

Hi Steve,

thanks for clarification. Would you suggest not to use 'DIMENSION' anymore as an attribute or is this just a matter of style?

0 Kudos
Highlighted
Black Belt Retired Employee
16 Views

It's just style. My own style is that I may use DIMENSION(:) with POINTER or ALLOCATABLE, but not with fixed dimensions. I'm not consistent about this, though.

0 Kudos