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

How to use an array of characters with unknown lengths as argument ?

François-Xavier
Beginner
1,049 Views
Hello,

I'm trying to write a routine which should accept an argument of the following type : an array of character string.
The length of the array is unknown and the length of each string is unknown.

Currently the syntax I'm using is:

SUBROUTINE FOO(XTAB)
C
CHARACTER*(*) XTAB
C
DIMENSION XTAB(*)

It compiles but during the execution I've some crash when I try to access the XTAB variable.

Is the syntax I'm using correct ? Why is it not working ?


I'm currently using the Intel Visual Fortran Version 11.1.

Thanks in advance for any help.
0 Kudos
2 Replies
anthonyrichards
New Contributor III
1,049 Views
Quoting - Franois-Xavier
Hello,

I'm trying to write a routine which should accept an argument of the following type : an array of character string.
The length of the array is unknown and the length of each string is unknown.

Currently the syntax I'm using is:

SUBROUTINE FOO(XTAB)
C
CHARACTER*(*) XTAB
C
DIMENSION XTAB(*)

It compiles but during the execution I've some crash when I try to access the XTAB variable.

Is the syntax I'm using correct ? Why is it not working ?


I'm currently using the Intel Visual Fortran Version 11.1.

Thanks in advance for any help.

I assume the lengthmust beknown to the caller at least, so why not send that as an argument? Also, I assume there must be a largest string length that is defined in the calling program, so why not declarean array of elements of just such a maximum length and send that too?

eg

SUBROUTINE FOO(CHARRAY, NELEMENTS, MAXLENGTH)
CHARACTER (LEN=MAXLENGTH) CHARRAY(NELEMENTS)

Ifsome array elements containcharacter strings that are less than MAXLENGTH, then I guess you need to use a terminating character to mark the end of the string, for example the CHAR(0) null character as used by C-strings.
This would mean that strings of length up to MAXLENGTH-1 could be stored, or MAXLENGTH-2 if two CHAR(0) terminator characters are used.

ps. in your code you have
C
CHARACTER*(*)
C

which I assume indicates that you are using fixed format and so the CHARACTER statement is ignored because it begins with 'C'?

CHARACTER*(*) is obsolete Fortran.
0 Kudos
Steven_L_Intel1
Employee
1,049 Views

First, Fortran doesn't have the concept of an array of character strings where each element has a different length. You can approximate this with an array of derived type where each derived type element is a CHARACTER(:), ALLOCATABLE. (You then have to allocate each string to the proper length - this can be done with an assignment to it.)

You don't say how you are passing XTAB to this routine and how the caller declares it. You also don't say what the "crash" is.
0 Kudos
Reply