Re: Accessing elements in foreign array of structs
Martin Simmons <[email protected]> Mon, 13 Apr 2026 16:26:48 +0100
| Newsgroups | gmane.lisp.lispworks.general |
|---|---|
| Message-ID | <[email protected]> |
Sorry, the FLI doesn't have the equivalent of the points[index].x syntax in C, mixing slot and array access, so you have to make a pointer to each struct somehow like in Julian's code. The only small improvement I can offer is to use (with-coerced-pointer (current) points ...) instead of (let ((current (copy-pointer points))) ...) to make the pointer object have dynamic extent. -- Martin Simmons LispWorks Ltd http://www.lispworks.com/ >>>>> On Mon, 13 Apr 2026 08:57:00 +0200, Erik Ronström (as erik dot ronstrom at doremir dot com) said: > > Thanks, that will also work! > > (Although I'm still puzzled; it feels like there *has to* be some way to > index directly into a struct member of an array!?) > > Erik > > > Den 2026-04-12 kl. 22:53, skrev Julian Baldwin (as julian at jbaldwin > dot net): > > Sorry spam (original reply didn’t include the mailing list), but does > > something like this work? > > > > (use-package "FLI") > > > > (define-c-struct (point (:foreign-name "tagPOINT")) > > (x :long) > > (y :long)) > > > > (with-dynamic-foreign-objects ((points point :nelems 3)) > > (let ((current (copy-pointer points))) > > (dotimes (index 3) > > (setf (foreign-slot-value current 'x :object-type 'point) index > > (foreign-slot-value current 'y :object-type 'point) index) > > (format t "~S: ~S ~S,~S~%" > > index > > current > > (foreign-slot-value current 'x) > > (foreign-slot-value current 'y)) > > (incf-pointer current)))) > > > > The compiler can optimise slot access if you pass the :object-type, > > but it’s not strictly necessary. > > > > Julian. > > > > > > ------ Original Message ------ > > From "Erik Ronström (as erik dot ronstrom at doremir dot com)" > > <[email protected]> > > To "LispWorks" <[email protected]> > > Date 13/4/2026 4:53:18 am > > Subject Accessing elements in foreign array of structs > > > >> Hi, > >> I am calling a foreign function that takes an array of points as input. > >> (fli:define-c-struct (point (:foreign-name "tagPOINT")) > >> (x :long) > >> (y :long)) > >> (let ((points (fli:allocate-foreign-object :type 'point :nelems 3))) > >> (the-foreign-function points)) > >> I can't figure out how to access the elements of the foreign array > >> directly. > >> This works: > >> (let ((p (fli:allocate-foreign-object :type 'point))) > >> (setf (fli:foreign-slot-value p 'x) 100 > >> (fli:foreign-slot-value p 'y) 200 > >> (fli:dereference points :index 0) p) > >> But how do I set the elements of points without using an intermediate > >> object? > >> Erik _______________________________________________ Lisp Hug - the mailing list for LispWorks users [email protected] http://www.lispworks.com/support/lisp-hug.html