For Programmers: Free Programming Magazines  


Home > Archive > Fortran > July 2006 > Why are allocatable indices in main different from those in subroutine? (1:n) to (0:









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Why are allocatable indices in main different from those in subroutine? (1:n) to (0:
Mike

2006-07-28, 4:00 am

Hi:

Why are allocatable indices in main different from those in
subroutine?

module defXY
contains
subroutine Bar(x,n)
real*4, allocatable :: x(:)
integer :: n
n=3
allocate(x(n))
do i=1,n
x(i)=i
end do
print *,'in sub:','n=',n
do i=1,n
print *,'in sub:',x(i)
end do
end subroutine Bar
end module defXY

program main
use defXY
real*4, allocatable :: x(:)
integer :: n=3
call Bar(x,n)
print *,'in main:','n=',n
do i=1,n
print *,'in main:',x(i)
end do
stop
end

Results:
in sub n=3
in sub:1.
in sub:2.
in sub:3.
in main n=3
in main:2.
in main:3.
Array bounds exceeded ........................ Why?

If I change sections in main into :

do i=0,n-1
print *,'in main:',x(i)
end do
Results:
in sub n=3
in sub:1.
in sub:2.
in sub:3.
in main n=3
in main:1.
in main:2.
in main:3.

OK now. why? thank you in advance.

Mike

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com