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

character array construction with variable length

Zuodong_Y_
Novice
4,631 Views

Here is the code:

program main
	implicit none
	character(:), allocatable :: i
	write(*,*)["1","1"//"2"]
	i="2"
	write(*,*)["1","1"//i]
end program

I expect this two write will give the same results. But the first one gives ["1 ","12"] while the second one truncate "1"//i to "1". I know this may due to the fact that at compile time compiler can not estimate the length of "1"//i so it make the length with the longest length it can measure (which is one in this case). I think this is quite unexpected and compiler should warn about it or just raise an error.

0 Kudos
24 Replies
jimdempseyatthecove
Honored Contributor III
644 Views

Thanks for the links. Both are very interesting.

Jim Dempsey

0 Kudos
FortranFan
Honored Contributor III
644 Views

Steve Lionel (Intel) wrote:

Um, UDDTIO is just for I/O - it has no other purposes. Ian was just saying that you can treat these "variable string types" as normal variables in READ/WRITE with a suitable UDDTIO procedure.

Steve,

You may also want to point out the acronym UDDTIO is not part of the standard, that any such naming might have been popularized by some on the committee in the internal/external communications but it is not an 'official' term.  Based on my check the standard only says "Defined input/output procedures" which is explanatory enough, I suppose.

Separately, you would have long noticed the employment of derived types (DT) to provide a variety of additional string type of functionalities beyond the intrinsic CHARACTER facility is common, including as suggested by IanH in this thread.  A common theme among Fortranners is the CHARACTER substring designator syntax with : is very neat and the users would also like to use it with any DT solution for string types.

Would you know if the standard committee aware of this and do you think there is any possibility that such a feature might tabled for consideration for the next revision i.e., the one AFTER Fortran 2015?  That is, would the standard committee be open to the idea of DEFINED DESIGNATOR bindings to DTs in a future standard, similar to defined operators which is provided in the current standard?  If not, if they have other ideas and proposals to benefit the users.  More specifically, I am asking if Intel reps can take this into consideration and bring it up on behalf of users such as me when the time is appropriate?

 

 

 

0 Kudos
Steven_L_Intel1
Employee
644 Views

I can't recall discussion of an idea such as that. I will pass it on to our other representatives (since my time on the committee is short.)

0 Kudos
Zuodong_Y_
Novice
644 Views

Hey guys,

I found another strange behaviour that related to variable length character array, here is the code:

program main
    implicit none
    character(:), allocatable :: i
    i="2"
    write(*,*)["1","1"//i] ! truncated
    write(*,*)["1",i//i] ! truncated
    write(*,*)["1",i,i//i] ! not truncated
end program

as you can see, the last write actually gives ["1 ","2 ","22"]. Why not give ["1","2","2"] as discussed before?

0 Kudos
Reply