array of strings

array of strings

    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?

18 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

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.

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



array = [string('jonas'), string('sssssssssss'), string('q')]



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.

Portrait de jimdempseyatthecove

>>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

yes, it is ASCII

What is the interface to the subroutine you're calling?

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

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.



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

character(len=:), dimension(:),  allocatable, array

is legal and if so exactly what it means.

yes that is accepted. The gfortran reported errors are those that I already mentioned.

Portrait de Steve Lionel (Intel)

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.


If that is correct why Intel fortran acepts it?

Portrait de Steve Lionel (Intel)

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.


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.

Portrait de Steve Lionel (Intel)

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' /)

Portrait de jimdempseyatthecove

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

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.

Connectez-vous pour laisser un commentaire.