- #1
shitij
- 19
- 0
Hi all !
I am new to fortran. Please see the following code. It is a simple code to pass an array to a subroutine and print it, but doesn't behave that way:
The output is:
Two questions:
1. Why is the size of array not being printed correctly in the subroutine?
2. Why the segmentation fault? Even array(1,1) can't be accessed?
Thank you in advance !
I am new to fortran. Please see the following code. It is a simple code to pass an array to a subroutine and print it, but doesn't behave that way:
Code:
program exp_realloc
implicit none
integer,allocatable,dimension(:,:):: array
integer::i,j
allocate(array(3,3))
write(*,*)size(array,1)," ",size(array,2)
do i=1,3
do j=1,3
array(i,j)=i*j
write(*,*)array(i,j)
enddo
write(*,*)
enddo
CALL func(array)
end program exp_realloc
subroutine func(array)
integer,dimension(:,:),intent(in)::array
integer::i,j
write(*,*)"********* Inside subroutine **********"
write(*,*)size(array,1)," ",size(array,2)
write(*,*)array(1,1)
write(*,*)array(1,2)
write(*,*)array(2,1)
do i=1,3
do j=1,3
write(*,*)array(i,j)
enddo
enddo
end subroutine func
The output is:
Code:
3 3
1
2
3
2
4
6
3
6
9
********* Inside subroutine **********
131097 1
Segmentation fault
Two questions:
1. Why is the size of array not being printed correctly in the subroutine?
2. Why the segmentation fault? Even array(1,1) can't be accessed?
Thank you in advance !