Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29602 ディスカッション

array of strings

Rodrigues__Pedro
ビギナー
5,202件の閲覧回数

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 件の賞賛
17 返答(返信)
Simon_Geard
新規コントリビューター I
5,202件の閲覧回数
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
Simon_Geard
新規コントリビューター I
5,202件の閲覧回数
or [fortran] array = [string('jonas'), string('sssssssssss'), string('q')] [/fortran] Simon
Rodrigues__Pedro
ビギナー
5,202件の閲覧回数
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.
jimdempseyatthecove
名誉コントリビューター III
5,202件の閲覧回数
>>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
Rodrigues__Pedro
ビギナー
5,202件の閲覧回数
yes, it is ASCII
Simon_Geard
新規コントリビューター I
5,202件の閲覧回数
What is the interface to the subroutine you're calling?
Rodrigues__Pedro
ビギナー
5,202件の閲覧回数
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
Arjen_Markus
名誉コントリビューター II
5,202件の閲覧回数
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
Simon_Geard
新規コントリビューター I
5,202件の閲覧回数
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.
Rodrigues__Pedro
ビギナー
5,202件の閲覧回数
yes that is accepted. The gfortran reported errors are those that I already mentioned.
Steven_L_Intel1
従業員
5,202件の閲覧回数
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.
Rodrigues__Pedro
ビギナー
5,202件の閲覧回数
If that is correct why Intel fortran acepts it?
Steven_L_Intel1
従業員
5,202件の閲覧回数
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.
Rodrigues__Pedro
ビギナー
5,202件の閲覧回数
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.
Steven_L_Intel1
従業員
5,202件の閲覧回数
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' /)
jimdempseyatthecove
名誉コントリビューター III
5,202件の閲覧回数
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
Rodrigues__Pedro
ビギナー
5,202件の閲覧回数
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.
返信