#Assign to optional by pointer

1 messages · Page 1 of 1 (latest)

echo hornet
#

If I have a C function that has a mutable pointer as a parameter which it uses as an output parameter (it sets the pointer's value without reading), is there any way I can set the output destination to an optional variable? The function looks like
fn fillValue(val: [*c]T) void
and I'm trying to do

var x: ?T = undefined;
fillValue(&x);

I can achieve this by using a temporary and copying into x, but is there a nicer way? It's also possible to get pointers from captures, but that requires the optional to be non-null initially.

frail summit
#

You cannot write to a Zig optional from C

#

because optionals don't have a defined memory layout

#

you know that they're kinda like a tagged union, but zig is allowed to change the in-memory representation as it sees fit

#

the only exception to this are pointers, so if your T it a pointer, then this could work