Hi,
I'm just starting with the FFI. I'm trying to call a C function that updates a pointer argument. However, that value does not seem to change.
My understanding is that all arguments passed to C are treated as immutable. So, if we need to return any value from C then, it cannot be via an argument only. Is that correct? Please see my example below.
(c-declare #<<c-declare-end
#include <stdio.h>
int get_value(int* value) { if(NULL != value) { *value = 42; } else { printf("Invalid pointer"); }
return 21; }
c-declare-end )
(c-define-type int* (pointer int))
(define get-value (c-lambda (int*) int "get_value"))
;; Define a Wrapper in C (define %get-value (c-lambda () int #<<C-END int tmp; int retval = get_value(&tmp);
/* TODO: Handle retval inside the C Wrapper code */
___result = tmp; C-END ))
;; Can we update a pointer via a c function call arg? (define value #f) (let ((retval (get-value value))) (if (eq? retval 21) (begin (display value) ; Prints #f (newline)) (begin (display "Did not get 21 as return value") (newline))))
; Now use the wrapper API instead (set! value (%get-value)) (display value) ; No surprise, prints 42 (newline)
Thanks, Sid