# Proper way to assign to \*anyopaque

**URL:** <https://ziggit.dev/t/proper-way-to-assign-to-anyopaque/17904>\
**Category:** Help\
**Tags:** type-system\
**Created:** [October 9, 2026, 7:23pm UTC](https://ziggit.dev/t/proper-way-to-assign-to-anyopaque/17904 "2026-10-09T19:23:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lfn1112](https://ziggit.dev/user_avatar/ziggit.dev/lfn1112/32/8426_2.png) [@lfn1112](https://ziggit.dev/u/lfn1112)\
**Post date:** [October 9, 2026, 7:23pm UTC](https://ziggit.dev/t/proper-way-to-assign-to-anyopaque/17904/1 "2026-10-09T19:23:59Z")

</div>

I’m trying to assign to a `void *` handed to me from sdl. Assigning to the dereferenced pointer is a type mismatch. @memset expects dest to be a slice. Is there an idiomatic way to do this kind of assignment? Can I cast \*anyopaque to a slice with a runtime-known size? Thanks in advance

```zig
const mappedTransferBuffer = sdl.SDL_MapGPUTransferBuffer(device, vertexTransferBuffer, false);
if(mappedTransferBuffer == null) sdl_err();
//@memset(mappedTransferBuffer.?, vertexData);
mappedTransferBuffer.?.* = vertexData;

```

---

<div class="post-metadata">

**Author:** ![abdullah-b-al](https://ziggit.dev/user_avatar/ziggit.dev/abdullah-b-al/32/4601_2.png) [@abdullah-b-al](https://ziggit.dev/u/abdullah-b-al)\
**Post date:** [October 9, 2026, 7:38pm UTC](https://ziggit.dev/t/proper-way-to-assign-to-anyopaque/17904/2 "2026-10-09T19:38:09Z")

</div>

If you want to cast `mappedTransferBuffer` you can do it as follows:

```zig
const ptr : [*]type_of_element = @ptrCast(@alignCast(mappedTransferBuffer));
const slice = ptr[0..len_of_buffer];

```

---

<div class="post-metadata">

**Author:** ![lfn1112](https://ziggit.dev/user_avatar/ziggit.dev/lfn1112/32/8426_2.png) [@lfn1112](https://ziggit.dev/u/lfn1112)\
**Post date:** [October 9, 2026, 7:52pm UTC](https://ziggit.dev/t/proper-way-to-assign-to-anyopaque/17904/3 "2026-10-09T19:52:19Z")

</div>

Many thanks! new code is compiling:

```zig
    const mappedTransferBuffer = sdl.SDL_MapGPUTransferBuffer(device, vertexTransferBuffer, false);
    if(mappedTransferBuffer == null) sdl_err();
    const mappedPtr: [*]@TypeOf(vertexData[0]) = @ptrCast(@alignCast(mappedTransferBuffer));
    const mappedSlice = mappedPtr[0..vertexData.len];
    mappedSlice.* = vertexData;

```

---

<div class="post-metadata">

**Author:** ![showell](https://ziggit.dev/user_avatar/ziggit.dev/showell/32/10287_2.png) [@showell](https://ziggit.dev/u/showell)\
**Post date:** [October 9, 2026, 9:06pm UTC](https://ziggit.dev/t/proper-way-to-assign-to-anyopaque/17904/4 "2026-10-09T21:06:16Z")

</div>

Is there a reason you don’t return for the null situation?

---

<div class="post-metadata">

**Author:** ![lfn1112](https://ziggit.dev/user_avatar/ziggit.dev/lfn1112/32/8426_2.png) [@lfn1112](https://ziggit.dev/u/lfn1112)\
**Post date:** [October 9, 2026, 9:19pm UTC](https://ziggit.dev/t/proper-way-to-assign-to-anyopaque/17904/5 "2026-10-09T21:19:48Z")

</div>

sdl\_err() panics so there’s no need

---

<div class="post-metadata">

**Author:** ![showell](https://ziggit.dev/user_avatar/ziggit.dev/showell/32/10287_2.png) [@showell](https://ziggit.dev/u/showell)\
**Post date:** [October 9, 2026, 9:24pm UTC](https://ziggit.dev/t/proper-way-to-assign-to-anyopaque/17904/6 "2026-10-09T21:24:19Z")

</div>

I would still do an explicit return or unreachable there.
