Re: accessing embedded structs

  • From: Mike Pall <mike-1205@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 28 May 2012 23:45:12 +0200

William Adams wrote:
> And would call it like this:
> 
> struct bar abar;
> 
> func(&abar.starting)
> 
> Is this how I would do this in LuaJIT? (assuming 'func()' is still on the 'C' 
> side)
> 
> local abar = ffi.new("struct bar");
> 
> local ending_offset = ffi.offsetof("struct bar", "ending")
> 
> local ptr = ffi.cast("struct bar *", abar)
> 
> ptr = ptr + ending_offset
> 
> func(ptr)

Nope, just call:

  func(abar.starting)
-or-
  func(abar.ending)

Accessing a nested struct creates a reference to the inner struct
(and not a copy of it). This can be treated just like the inner
struct or a pointer to the inner struct (e.g. when passed to a C
function).

And yes, if func() modifies fields of its argument, this _does_
modify parts of 'abar'.

--Mike

Other related posts: