#aHow to use this C code?

1 messages · Page 1 of 1 (latest)

near fog
#

Quick question, what's the equivalent of this C code in Zig?
(void *) 12345

The whole line:

res = uvc_start_streaming(devh, &ctrl, cb, (void *) 12345, 0);

From here: https://libuvc.github.io/libuvc/

It looks like the lib wants me to pass in a void pointer so I guess I have to do that first. How to do this style of void pointer in Zig? Then I have to cast it to the real one within the callback function.

#

It looks like this value just comes from nowhere. It's confusing me.

olive lichen
#

the equivalent code would be @ptrFromInt(12345), but yes, that pointer does not point to anything valid

polar wharf
#

It's user callback data, so you can pass anything you want, and the library will not inspect the value, but rather pass the same value directly back to you in the callback.

near fog
#

Inside the function that I'm transcribing it looks like this:

enum uvc_frame_format *frame_format = (enum uvc_frame_format *)ptr;
polar wharf
#

void* is most general in that it can point to an arbitrary amount of data anywhere in memory

near fog
#

Yes. If I just follow the example, it's essentially just dropping in an integer value?

polar wharf
#

but you can also use @ptrFromInt and @intFromPtr to pass a single arbitrary usize number instead without needing to store the number somewhere stable in memory

#

In all cases, it is up to the callback implementation to know what type was passed, in order to correctly cast it back to the exact same type.