Hi,
I recently ran across the following problem and even though I seem
to have found the solution, I’m curious if the pgf90 compiler is following
the fortran standard, or if it’s a kind of bug. My previous compiler (sgi)
accepted the code and ran correctly, but then again that compiler is
known for accepting almost anything.
I’m trying to pass an array of character strings as a pointer
to a subroutine, and allocate it within the subroutine.
In the mainline, it’s declared as:
character(len=100), dimension(:), pointer :: data
If I declare it as a character(len=*) in the subroutine, then I get a
null pointer error when I try allocating it. When I change the
subroutine interface to pass in the string length and then
declare the array as:
integer, intent(in) :: nchar
character(len=nchar), dimension(:), pointer :: data
Then the program works. A short test program is attached.
Thanks for any insight,
Catherine
MODULE MOD1
CONTAINS
SUBROUTINE SUB_CHAR(DATA,NCHAR)
IMPLICIT NONE
INTEGER, INTENT(IN) :: NCHAR
CHARACTER(LEN=NCHAR), DIMENSION(:), POINTER :: DATA
INTEGER :: I
i = 1
allocate(data(i))
RETURN
END SUBROUTINE SUB_CHAR
SUBROUTINE SUB_INT(DATA)
IMPLICIT NONE
INTEGER, DIMENSION(:), POINTER :: DATA
INTEGER :: I
i = 1
allocate(data(i))
RETURN
END SUBROUTINE SUB_INT
END MODULE MOD1
PROGRAM PASS_POINTERS
USE MOD1, ONLY: SUB_CHAR, SUB_INT
CHARACTER(LEN=100), DIMENSION(:), POINTER :: DATA_CHAR
INTEGER, DIMENSION(:), POINTER :: DATA_INT
call sub_char(data_char,100)
call sub_int(data_int)
write(,) 'size_char = ',size(data_char,1)
write(,) 'size_int = ',size(data_int,1)
STOP
END PROGRAM PASS_POINTERS