here the basics of allocated characters are discussed:
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 18.104.22.168 [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 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.
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.)
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.
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...
DIMENSION is for declarations - it's fine there, though is generally considered old-fashioned. It's not for ALLOCATE.
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.