#How to cast a variable as [*c] to input into a c library function. Raylib-zig 4.5 with Zig 10.1

1 messages · Page 1 of 1 (latest)

mighty copper
#

error: expected type '[*c]raylib-zig. Camera3D', found 'raylib-zig.Camera3D' rl.UpdateCamera(camera); ^~~~~~
My understanding is that
rl.UpdateCamera([:0]camera);
is close to what I need to do, but I get this error
error: expected type 'type', found 'raylib-zig.Camera3D'
I've tried other work arounds, like using zig master and raylib.zig, but that is giving me an even more confusing
error: unexpected
When I try to build the default zig init-exe.
My other thought was to write a little .c helper function that wouldn't have this problem, but I still need to pass in that camera to it, and I think I'll have the same problem all over again.

Is this bug fix related to this problem? Its why I tried the master branch of zig, but I couldn't tell if it would work because of the Unexpected error
https://github.com/ziglang/zig/commit/6511afcfe090f26345873e7e8db3ae301f8a18a7#diff-62bbd081beb069ea40c30bca57c12a38b79cb1ed5a2bcf4e20be63a5335788c7

lone sable
#

&camera

wet token
#

The problem here is that you have a Camera3D which is a value type.
The function wants a [*c]Camera3D which is a pointer type.
In order to get the pointer to camera you need to use the address-of operator &:

rl.UpdateCamera(&camera);
mighty copper
#

I actually started off with that, it give the same error as without the reference
error: expected type '[*c]raylib-zig.Camera3D', found '*const raylib-zig.Camera3D' rl.UpdateCamera(&camera); ^~~~~~~ D:\Projects\RaylibZig\raylib-zig\examples\core\dash.zig:67:25: note: cast discards const qualifier D:\Projects\RaylibZig\raylib-zig\lib\raylib-wa.zig:239:37: note: parameter type declared here pub extern fn UpdateCamera(camera: [*c]Camera) void; ~^~~~~~~~~

#

And for completeness this was the other obvious attempt.
error: expected type 'type', found 'raylib-zig.Camera3D' rl.UpdateCamera([*c]camera); ^~~~~~
I've tried pretty much every combo I could think of

lone sable
#

That means you declared it as const but the function wants a mutable version

mighty copper
#

Also tried casting
const cam = @ptrCast([*]rl.Camera, @alignCast(@alignOf(rl.Camera), &camera));

#

Wow I'm dumb

#

Thank you

lone sable
#

You'll run into this a lot in C land. Zig is a lot more anal about const pointers

mighty copper
#

Yea, I think the [ *c] vs *const just really through me off the right track. Is [*c] just a C pointer? I was thinking I needed to cast camera to an array of size 1 or something

lone sable
#

There's talk renaming [*c] to [*ambiguous] or something. It means we don't know whether it's a single pointer or multi pointer (just like C!)