我发现这段代码行为异常
module testmodule
integer, parameter :: LCHARS = 50
contains
subroutine init()
call foobar("foobar")
end subroutine
subroutine foobar(s)
character(len=*), intent(in) :: s
call bar(s)
end subroutine
subroutine bar(str)
character(len=LCHARS), intent(in) :: str
print *, str
end subroutine
end module
program foo
use testmodule
call init()
end program
此代码打印依赖于编译器的垃圾。
我看到问题在于我正在跳过带有len=*
字符串参数的例程,然后将其传递给字符串参数指定长度的例程。
幕后到底发生了什么,标准中在哪里描述了这种行为?我是否应该避免为字符例程参数指定长度,因为这种行为可能随时发生而没有警告?