Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

character array index error

Blane_J_
New Contributor I
89 Views

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
FortranFan
Honored Contributor II
89 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

 

Blane_J_
New Contributor I
89 Views
Thanks FortranFan, I'll do it.
Reply