[Fortran] 纯文本查看 复制代码
program test_pointer
implicit none
real, dimension(100,100), target :: array1, array2
real, dimension(:,:), pointer :: p1, p2, temp_p
real, dimension(100,100) :: a1, a2, temp
integer :: i
real :: begin, end
call random_number(a1)
call random_number(a2)
array1=a1
array2=a2
p1=>array1
p2=>array2
call cpu_time(begin)
do i=1,1000000
temp=a1
a1=a2
a2=temp
end do
call cpu_time(end)
write(*,*) 'Time of variable exchange:', end-begin
call cpu_time(begin)
do i=1,1000000
temp_p=>p1
p1=>p2
p2=>temp_p
end do
call cpu_time(end)
write(*,*) 'Time of pointer exchange:', end-begin
end program test_pointer
[Fortran] 纯文本查看 复制代码
real , target :: rAmp , rPhz
real , pointer :: p
if ( ... )
p => rAmp
else
p => rPhz
end if
call proc( p )
p = func( p )
call write( p )
[Fortran] 纯文本查看 复制代码
type pST_Array
real , pointer :: p( : )
end type pST_Array
Type ( pST_Array) , pointer :: pA( : )
integer :: i , j
Allocate( pA( 10 ) ) !// 产生10个指向数组的指针。
Do i = 1 , 10
Allocate( pA(i)%p(2) ) !// 每个指针指向一个2元素数组
Do j = 1 , 2
pA(i)%p(j) = i*100+j
End Do
End Do
Do i = 1 , 10
write( * , * ) pA(i)%p( : )
End Do
!// 尚未书写释放指针代码
End Program
[Fortran] 纯文本查看 复制代码
real ,pointer :: p
real , target :: a( 10 )
integer :: i
Do i = 1 , 10
p => a(i)
End Do