0

I've got a simple Fortran function that converts a comma delimited string into an array of character(len=255). I can't figure out how to create a variable that can receive the output of this function.

Here's my comma delimited string to array function:

    function CommaDelimitedStringToArray(commastr) result (slist)

       character(*), intent(in)  :: commastr

       ! Local vars
       character(255), dimension (:), allocatable :: slist
       integer :: scount

       ! Lots of code removed - I set scount to be the number 
       ! of elements in the comma delimited string, with a 
       ! minimum value of 1

       ! Create the slist and populate it...
       allocate (slist(scount))

       ! Lots more code removed.  At the end of this function I know I have
       ! slist defined with the correct size and correct contents

end function CommaDelimitedStringToArray

Now I want to actually use this function. I've tried this:

    character(255) :: commastr
    character(255), dimension (:), allocatable :: mylist

    commastr = "this,that,theother,etc"

    mylist = CommaDelimitedStringToArray(trim(commastr))

But mylist does not receive the array from the function call.

In my testing if I allocate mylist to the correct size before calling the function it does receive the array correctly. But, obviously, in the real world I don't know the size before I call the function.

How do I declare/use mylist so it will correctly receive the array?

EDIT - My compiler reports itself as "Intel® Parallel Studio XE 2016 Update 3 Composer Edition for Fortran Windows Integration for Microsoft Visual Studio 2015, Version 16.0.0062.14" running on Win 10.

Compiler flags are:

/nologo /debug:full /Od /warn:interfaces /module:"Debug\" /object:"Debug\" /Fd"Debug\vc140.pdb" /traceback /check:bounds /check:stack /libs:static /threads /dbglibs /c

SAL
  • 1,218
  • 1
  • 14
  • 34
  • Your code is completely invalid in Fortran 90. Why do you use the fortran90 tag? Are you limited by the old and obsolete Fortran 90 in any way? The code is completely fine Fortran 2003. There is nothing more you have to do, it should work as it stands. – Vladimir F Героям слава Aug 17 '17 at 12:17
  • My knowledge of Fortran variants is inadequate. I'll remove the f90 tag. However the code doesn't work in the Intel Fortran (Windows version) compiler. I get 'undefined pointer/array' for mylist in the debugger. – SAL Aug 17 '17 at 12:23
  • Please report your compiler version and the flags and commands you use for compilation. It may be a well-known issue with Intel Fortran before v17. It may require the `-standard-semantics` flag. It is not a bug in the compiler, but a design choice they made and then later changed. – Vladimir F Героям слава Aug 17 '17 at 12:23
  • Please check the solution in the links does indeed work for you. The question can be re-opened if not. – Vladimir F Героям слава Aug 17 '17 at 12:28
  • Thanks Vladimir F - the -standard-semantics option fixed it... – SAL Aug 17 '17 at 12:49

0 Answers0