Download CFFI User Manual - Common

Transcript
Chapter 10: Functions
Examples
(defcfun "strlen" :int
"Calculate the length of a string."
(n :string))
CFFI> (strlen "123")
⇒ 3
(defcfun ("abs" c-abs) :int (n :int))
CFFI> (c-abs -42)
⇒ 42
Function without arguments:
(defcfun "rand" :int)
CFFI> (rand)
⇒ 1804289383
Variadic function example:
(defcfun "sprintf" :int
(str :pointer)
(control :string)
&rest)
CFFI> (with-foreign-pointer-as-string (s 100)
(sprintf s "%c %d %.2f %s" :char 90 :short 42 :float pi
:string "super-locrian"))
⇒ "A 42 3.14 super-locrian"
See Also
[foreign-funcall], page 91
[foreign-funcall-pointer], page 93
90