Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26734 Discussions

character array allocation, alternative definition fails

Johannes_Rieke
New Contributor III
128 Views

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
Arjen_Markus
Valued Contributor III
128 Views

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

Johannes_Rieke
New Contributor III
128 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.

Arjen_Markus
Valued Contributor III
128 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.)

andrew_4619
Honored Contributor I
128 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.

 

 

 

 

Johannes_Rieke
New Contributor III
128 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...

Steve_Lionel
Black Belt Retired Employee
128 Views

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

Johannes_Rieke
New Contributor III
128 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?

Steve_Lionel
Black Belt Retired Employee
128 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.

Reply