Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
14 Views

character array index error

Hi, I want to show the following code which a character array index error occurs.

module test1
    implicit none
    
    private
    public :: tp1, const1, const2, const3, chars

    type :: tp1
        private
        integer :: var
    contains
        private
        procedure, public, pass :: getvalue
    end type tp1
    
    type(tp1), parameter :: const1 = tp1(0)
    type(tp1), parameter :: const2 = tp1(1)
    type(tp1), parameter :: const3 = tp1(2)

    character(*), dimension(0:*), parameter :: chars = ["1234", "5678", "9012" ]
    
    contains
    
    function getvalue(this)
        implicit none
    
        integer                :: getvalue
        class(tp1), intent(in) :: this
    
        getvalue = this % var
    
    end function getvalue
    
end module test1
    
program main    
    use test1
    implicit none
    
    integer :: idx

    print*, chars(0), ' ', chars(1), ' ', chars(2)  ! <---Output: 1234 5678 9012
    
    idx = const1 % getvalue()
    print*, idx                                     ! <---Output: 0

    print*, chars(idx)                              ! <---Output: 1234
    print*, chars(const1 % getvalue())              ! <---Output: 5678
    print*, chars(const2 % getvalue())              ! <---Output: 5678
    print*, chars(const3 % getvalue())              ! <---Output: 5678
    print*, chars(get())                            ! <---Output: 1234
    
    contains
    
    function get()
        implicit none
    
        integer :: get
    
        get = 0
    
    end function get
    
end program main

The 2-4 PRINT statements of chars gives a strange output of chars(1) rather than the expected chars(0), all other PRINTs are all correctly printed. Is this a mistake or something ?

0 Kudos
2 Replies
Highlighted
Valued Contributor III
14 Views

Clearly a bug in Intel Fortran with what it is doing with NAMED CONSTANTS and the code it generates for it.  The problem persists in current 2019 Beta version too.

I suggest you submit this to Intel support, you may want to consider a minimal example:

    use test1, only : chars, const3

    integer :: idx

    idx = const3%getvalue()
    print *, "idx = ", idx, "; expected is 2"
    print *, "chars( const3%getvalue() ) yields ", chars( const3%getvalue() ), "; expected is ", chars(2)

end

for which another compiler correctly yields,

 idx =            2 ; expected is 2
 chars( const3%getvalue() ) yields 9012; expected is 9012

 

0 Kudos
Highlighted
New Contributor I
14 Views

Thanks FortranFan, I'll do it.
0 Kudos