Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28456 Discussions

Dynamic allocation of Fortran strings

hamid3252
Beginner
1,216 Views
Hi,

1- I am trying to define a string with dynamic length at the beginning of a subroutine as the line declared here:
here is my decleration:
character(len=:), allocatable::stringBuff(:)
But I am getting this error:
SysCallTest.f90(314): error #5082: Syntax error, found ':' when expecting one of: ( * ...
character(len=:), allocatable::stringBuff(:)
----------------^
I even tried to use this syntax:
character*(*)::stringBuff1
But again I got a similar error.
SysCallTest.f90(315): error #6832: This passed length character name has been used in an invalid context. [STRINGBUFF1]
character*(*)::stringBuff1
-----------------^
May I ask why it doesn't accept my definition?
2- In C++ we compare two strings using other functions like strcmp. Is the operator = overloaded for strings (char arrays) in fortran or I have to rely on another way of comparing?
I am using Fortran 11.0.....
Thanks for your help,
Hamidreza,
0 Kudos
4 Replies
mecej4
Honored Contributor III
1,216 Views
Context matters. It is not possible to help when you show only a single-line declaration. The following trivial program can be compiled without error messages (with the 12.x compilers):

[fortran]character(len=:), allocatable::stringBuff(:)
end
[/fortran]
0 Kudos
hamid3252
Beginner
1,216 Views
Please try to compile this simple code using ifort:
[fortran]subroutine testSub(expectedPhrase)
  character*(*), intent(in)::expectedPhrase
  character(len=:), allocatable::stringBuff(:)
  
  stringBuff = expectedPhrase
end subroutine testSub[/fortran]
Here is the error I got:
SysCallTest_01.f90(3): error #5082: Syntax error, found ':' when expecting one of: ( * ...
character(len=:), allocatable::stringBuff(:)
----------------^
SysCallTest_01.f90(3): error #6832: This passed length character name has been used in an invalid context. [STRINGBUFF]
character(len=:), allocatable::stringBuff(:)
---------------------------------^
compilation aborted for SysCallTest_01.f90 (code 1)
0 Kudos
mecej4
Honored Contributor III
1,216 Views
I just noticed that you are using the 11.0 compiler. It probably does not support parameterized derived types -- a Fortran 2003 feature, an instance of which is used by the present program.

Either use a more recent issue of the compiler or do not use parameterized derived types.
0 Kudos
Steven_L_Intel1
Employee
1,216 Views
This is not really parameterized derived types - it is deferred-length character allocatable. This is supported in 11.1 but not earlier.
0 Kudos
Reply