Skip to content

Commit 6cd1b10

Browse files
committed
Fortran check if C function returned NULL pointer
1 parent 94f1f02 commit 6cd1b10

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

test/time/strptime.f90

+9-10
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,21 @@ program test_strptime
1010
end type
1111

1212
interface
13-
integer(C_INT) function strptime(str,format,tm) bind(C)
14-
import C_INT, C_CHAR, tm_struct
15-
character(kind=c_char), intent(in) :: str(*), format(*)
13+
function strptime(str,format,tm) bind(C)
14+
import
15+
character(kind=C_CHAR), intent(in) :: str(*), format(*)
1616
type(tm_struct), intent(out) :: tm
17+
type(C_PTR) :: strptime
1718
end function
1819
end interface
1920

20-
integer(C_INT) :: rc
21-
character(kind=c_char, len=20), parameter :: str = '2018-01-01 12:00:00'
22-
character(kind=c_char, len=18), parameter :: fmt = '%Y-%m-%d %H:%M:%S'
21+
type(C_PTR) :: rc
22+
character(kind=C_CHAR, len=20) :: str = '2018-01-01 12:00:00'
23+
character(kind=C_CHAR, len=18), parameter :: fmt = '%Y-%m-%d %H:%M:%S' // c_null_char
2324
type(tm_struct) :: tm
2425

25-
rc = strptime(trim(str)// c_null_char, trim(fmt)// c_null_char, tm)
26-
if(rc == 0) then
27-
error stop "strptime failed"
28-
end if
26+
rc = strptime(trim(str)// c_null_char, fmt, tm)
27+
if(c_associated(rc, c_null_ptr)) error stop "strptime failed"
2928

3029
print '(a3,1x,i4,1x,i2,1x,i2,1x,i2,1x,i2,1x,i2)', "OK:", &
3130
tm%tm_year+1900, tm%tm_mon+1, tm%tm_mday, tm%tm_hour, tm%tm_min, tm%tm_sec

0 commit comments

Comments
 (0)