miguel__frtt
miguel__frtt

Reputation: 73

Accessing members of a struct in array Clisp

Imagine I have a function that receive a array of structs like this:

(defun name-of-func (array) 
  (dotimes (i (array-total-size array))
    (print (aref array i))))

and the stuct is something like this

(defstruct sTSP 
  cidade 
  x 
  y)

How can I access the field x on i position of the array?

Upvotes: 1

Views: 113

Answers (1)

sds
sds

Reputation: 60004

Please take a look at the definition of defstruct. It is long, but well worth the read. If you are lazy, like we all are, search for reader:

(defstruct foo x y z)
(defparameter foo (make-foo :x 2 :y 4))
(foo-x foo)
==> 2
(foo-y foo)
==> 4
(foo-z foo)
==> NIL

PS1. Please note that array-total-size should not be used with aref but rather with row-major-aref. The difference is with multi-dimensional arrays which are implemented as vectors under the hood. E.g., your function will fail on (make-array '(2 2) :initial-element (make-sTSP)).

PS2. I re-use foo for both type name and variable name to illustrate that they reside is different name spaces.

Upvotes: 3

Related Questions