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

array of strings

Rodrigues__Pedro
Beginner
3,709 Views

Hello
    I am using gfortran and INtel I did this:

     Module variables
     character(len=:), dimension(:),  allocatable, array
     end module variables

    Sub test
    use variables
    integer (max_len)
    max_len=len_trim("something here")
    if(.not.allocated(array)) &
    allocate(character = max_len :: array(3))
   array = (/'jonas', 'sssssssssssssss','q' /)
   end sub test

   gfortran compilation gives this errors:

  - max_len can not be used in this context:
     allocate(character = max_len :: array(3)) allocate(character = max_len :: array(3))

  - Different character lenghts in array constructor: array = (/'jonas', 'sssssssssssssss','q' /)

   I solved this using a number instead of max_len for the first error;
   The second error can only be solved if all strings have the same lenght but this is not desired.

   with intel compiler no error is displayed. Why does gfortran report these errors?

0 Kudos
17 Replies
Simon_Geard
New Contributor I
3,709 Views
I didn't think you could do this. I thought the correct way was to define a type and declare an array of that type, e.g. [fortran] module variables type string character(len=:), allocatable :: str end type string end module variables program test use variables integer :: i type(string) :: array(3) array(1)%str = 'jonas' array(2)%str = 'sssssssssss' array(3)%str = 'q' write(*,'(a)') (array(i)%str,i=1,3) end program test [/fortran] Simon
0 Kudos
Simon_Geard
New Contributor I
3,709 Views
or [fortran] array = [string('jonas'), string('sssssssssss'), string('q')] [/fortran] Simon
0 Kudos
Rodrigues__Pedro
Beginner
3,709 Views
Thanks This is a good idea but I can't us this because my array can not be of derived type. I have to use it as a argument to a subroutine to pass the array contents to an menu field and a derived type as argument does not work there.
0 Kudos
jimdempseyatthecove
Honored Contributor III
3,709 Views
>>I have to use it as a argument to a subroutine to pass the array contents to an menu field and a derived type as argument does not work there. Does this also require your strings be ASCIIZ (null terminated)? IOW the menu routine is expeciting an array of C style string pointers. Jim Dempsey
0 Kudos
Rodrigues__Pedro
Beginner
3,709 Views
yes, it is ASCII
0 Kudos
Simon_Geard
New Contributor I
3,709 Views
What is the interface to the subroutine you're calling?
0 Kudos
Rodrigues__Pedro
Beginner
3,709 Views
The interface belongs to winteracter third party software, so, it has some arguments and one of them is the strings_array. Of course it is impossible to change winteracter core to read a derived type
0 Kudos
Arjen_Markus
Honored Contributor II
3,709 Views
It is actually an extension in Intel Fortran (up to F2003 or F2008) and in newer standards it is acceptable if you use: array = (/ character(len=len(array)):: 'jonas', 'sssssssssssssss','q' /) (so explicitly pass the length) The thing is that the length of a string is part of its type. So: "str" is of type character(len=3) and "string" is of type character(len=5). The version of gfortran you are using may not have caught up yet, thoguh version 4.6.1 does accept it. Regards, Arjen
0 Kudos
Simon_Geard
New Contributor I
3,709 Views
I understand that, it's just that the more information you can give the more likely it is someone will be able to help. If you can show the interface then we know exactly what we're trying to achieve rather than speculating. I'd also like to know if the declaration [fortran] character(len=:), dimension(:), allocatable, array [/fortran] is legal and if so exactly what it means.
0 Kudos
Rodrigues__Pedro
Beginner
3,709 Views
yes that is accepted. The gfortran reported errors are those that I already mentioned.
0 Kudos
Steven_L_Intel1
Employee
3,709 Views
sgeard, that is an array of "deferred-length" allocatable character variables, a Fortran 2003 feature. It allows the character length to change at run-time, and this can happen automatically through assignment. Note, though, that each element of the array has the same length - it is not an array of individually variable length strings. If that's what you want, you have to do it as an array of derived type where the type contains a charcter(:), allocatable component.
0 Kudos
Rodrigues__Pedro
Beginner
3,709 Views
If that is correct why Intel fortran acepts it?
0 Kudos
Steven_L_Intel1
Employee
3,709 Views
I don't understand the question. Intel Fortran supports the feature from the standard, and has for a few releases now. In the context of the array constructor with different lengths - we have an extension where we pad shorter strings to the longest length.
0 Kudos
Rodrigues__Pedro
Beginner
3,709 Views
What I saying is that Intel compiler accepted the code of my first post entirely with no warning message or error. As I said before gfortran doesn't.
0 Kudos
Steven_L_Intel1
Employee
3,709 Views
Well, no, the Intel compiler certainly did NOT accept the code of your first post - it contains numerous syntax errors. I suspect you attempted to create a paraphrase from memory and did not try to compile the code you posted. The statement: allocate(character = max_len :: array(3)) was probably meant to read: allocate(character(max_len) :: array(3)) Intel Fortran accepts this - it is standard Fortran 2003. The array constructor, as I noted before, is non-standard, but we accept it as an extension. If you compile with standards checking you'd get a warning such as: warning #8208: If type specification is omitted, each ac-value expression in the array constructor of type CHARACTER must have the same length type parameters. ['jonas'] array = (/'jonas', 'sssssssssssssss','q' /) -------------^ One can force all the elements to the same length in a standard-conforming manner like this: array = (/character(max_len) :: 'jonas', 'sssssssssssssss','q' /)
0 Kudos
jimdempseyatthecove
Honored Contributor III
3,709 Views
me>>Does this also require your strings be ASCIIZ (null terminated)? you>>yes, it is ASCII What about the 'Z' of the ASCIIZ? And, does your menu code expect C style array of pointers to ASCIIZ (with Z) strings? If so, then you need to code differently. Jim Dempsey
0 Kudos
Rodrigues__Pedro
Beginner
3,709 Views
Thanks for your help. I copied the code from my mind. Of course I compiled that using intel with no error and after that I wrote the post. Concerning the asciiz directive I only can say that the menu code does not pass any argument as pointer.
0 Kudos
Reply