Assigning values to array of pointers in Fortran -
i assign values array of pointers in fortran 2003. array of pointers realised means of derived data type arrpntr. please take @ simple example below:
program tst_pntr real, allocatable, target :: arr1(:), arr2(:) ! array of pointers type :: arrpntr real, pointer :: arr(:) => null() end type type(arrpntr) :: p(2) integer :: err integer, parameter :: m = 10, n = 10 allocate(arr1(m), arr2(n), stat=err) arr1 = [ (i, i=1, 10) ] arr2 = arr1**2 ! arr1 = arr1-0.5 ! p(1)%arr => arr1 p(1)%arr => arr1-0.5 p(2)%arr => arr2 write(*,*) p(1)%arr write(*,*) p(2)%arr arr1 = arr1**2 arr2 = sqrt(arr2) write(*,*) p(1)%arr write(*,*) p(2)%arr deallocate(arr1, arr2, stat=err) nullify(p(1)%arr, p(2)%arr) end program tst_pntr in current state compiler throws error: pointer assignment target neither target nor pointer @ (1) complaining line p(1)%arr => arr1-0.5. works in case split operation 2 parts in commented lines above. however, achieve same result without storing intermediate result in temporary variable arr1. operation arr1-0.5 simple example. in reality use more complex expression on right hand side of p(1)%arr =>. there better way avoiding use of intermediate array?
Comments
Post a Comment