File: pointer_to_cptr.f90

package info (click to toggle)
lfortran 0.58.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 54,512 kB
  • sloc: cpp: 162,179; f90: 68,251; python: 17,476; ansic: 6,278; yacc: 2,334; sh: 1,317; fortran: 892; makefile: 33; javascript: 15
file content (29 lines) | stat: -rw-r--r-- 738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
program pointer_to_cptr
    use iso_c_binding, only: c_ptr, c_loc, c_f_pointer, c_int
    implicit none

    integer, target :: arr(5) = [1, 2, 3, 4, 5]
    type(c_ptr) :: arr_ptr

    arr_ptr = c_loc(arr)

    call process_array(arr_ptr, size(arr))

contains

    subroutine process_array(ptr, n)
        use iso_c_binding, only: c_ptr, c_f_pointer, c_int
        type(c_ptr), intent(in) :: ptr
        integer, intent(in) :: n
        integer(c_int), pointer :: f_arr(:)
        integer :: i

        call c_f_pointer(ptr, f_arr, [n])

        print *, "Array elements from process_array:"
        do i = 1, n
            print *, "Element", i, "=", f_arr(i)
        end do
    end subroutine process_array

end program pointer_to_cptr