Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
27153 Discussions

Get the size of Character array that is passed to another subroutine

BilalShah
Beginner
194 Views

Dear All,

This is my first question and I have limited knowledge of Fortran, so please ignore my ignorance.

I need to know the size of a character array which is passed to a subroutine. Thanks in Advance. Example below:

 

     subroutine Main(ierror)

     character*25 strga(1)
     character*15 strgb
     

     call subA(strga(1),strgb,ierror)

     return
     end

     subroutine subA(tstra,tstrb,ierr)

!    This is where I need to know the length of tstra and tstrb (without 
!    having to explicitly passed by the calling routine)
!    I need the lengths so that I should not write more characters than
!    the length of the string

     return
     end
0 Kudos
1 Reply
Arjen_Markus
Valued Contributor III
187 Views

You can do the following:

 

subroutine subA(tstA,tstB,ierr)
    character(len=*) :: tstA, tstB
    integer :: ierr
 
    ierr = 0
    if ( len(tstA) < len(tstB) ) then
        ierr = 1
        return
    endif
    ...
end subroutine subA

 

The declaration of the arguments uses the indication "(len=*)" for the string length, which means it comes from the caller and the function len() returns the length of the string, that is, the maximum number of characters that can be stored. If you want the number of characters without the trailing blanks, then use len_trim().

(Of course, there is a lot more to be said about this, but this should get you a step further ;))

Reply