I read that Fortran (unlike C) knows the size of arrays, and can infer it when I pass the array into function (subroutine). But I cannot make it work.
Minimum example:
program.f90
program test_arrayPass
! === variables
integer :: n
real, dimension (:,:),allocatable :: data
! === Body
n = 3
allocate(data(n,n))
write (*,*) "DEBUG test_arrayPass : shape(data) ", shape(data)
call writeToArray( data )
end
writeToArray.f90
subroutine writeToArray( data )
real , dimension(:,:), intent(out) :: data ! none of these works
!real , dimension(:,:), intent(in) :: data ! none of these works
!real , dimension(*,*), intent(in) :: data ! none of these works
write (*,*) "DEBUG writeToArray : shape(data) ", shape(data)
end
Makefile
FC := /usr/bin/gfortran
FCFLAGS = -g -Og -c -fdefault-real-8 -fbacktrace -fno-align-commons -fbounds-check
FLFLAGS =
SRCS = $(patsubst %.f90, %.o, $(wildcard *.f90))
PROGRAM = program
all: $(PROGRAM)
$(PROGRAM): $(SRCS)
$(FC) $(FLFLAGS) -o $@ $^
%.o: %.f90
$(FC) $(FCFLAGS) -o $@ $<
clean:
rm -f *.o *.mod
Result
with dimension(:,:)
it does not know corret size inside writeToArray()
DEBUG test_arrayPass : shape(data) 3 3
DEBUG writeToArray : shape(data) 1 0
with dimension(*,*)
it produces error
5 | real , dimension(*,*), intent(in) :: data
| 1
Error: Non-PARAMETER symbol ‘data’ at (1) cannot be implied-shape