File: c_ptr_08.f90

package info (click to toggle)
lfortran 0.60.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,412 kB
  • sloc: cpp: 173,406; f90: 80,491; python: 17,586; ansic: 9,610; yacc: 2,356; sh: 1,401; fortran: 895; makefile: 37; javascript: 15
file content (35 lines) | stat: -rw-r--r-- 977 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
30
31
32
33
34
35
program c_ptr_08
    use iso_c_binding
    implicit none

    character(kind=c_char, len=*), parameter :: hex_str = "00FF" // c_null_char
    integer(c_long) :: result

    result = c_strtol(hex_str)
    print *, "Result:", result
    if (result /= 255_c_long) error stop

contains

    integer(c_long) function c_strtol(string)
        use iso_c_binding
        character(kind=c_char, len=*), intent(in), target :: string

        interface
            integer(c_long) function strtol(str, endptr, base) bind(C, name="strtol")
                use iso_c_binding
                type(c_ptr), intent(in), value :: str
                type(c_ptr), intent(inout) :: endptr
                integer(c_int), intent(in), value :: base
            end function strtol
        end interface

        type(c_ptr) :: str1, strend

        str1   = c_loc(string)
        strend = c_null_ptr

        c_strtol = strtol(str1, strend, 16_c_int)
    end function c_strtol

end program c_ptr_08