# How to anonymously initialize a slice?

**URL:** <https://ziggit.dev/t/how-to-anonymously-initialize-a-slice/5687>\
**Category:** Help\
**Tags:** language\
**Created:** [August 19, 2024, 4:57am UTC](https://ziggit.dev/t/how-to-anonymously-initialize-a-slice/5687 "2024-08-19T04:57:58Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![AndrewCodeDev](https://ziggit.dev/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@AndrewCodeDev](https://ziggit.dev/u/AndrewCodeDev)\
**Post date:** [August 19, 2024, 5:34am UTC](https://ziggit.dev/t/how-to-anonymously-initialize-a-slice/5687/6 "2024-08-19T05:34:10Z")

</div>

Yeah, I gotcha. I’m going to write out a few things that you are probably aware of but through the context of Zig.

Conceptually, based on the function you’ve provided, this isn’t even really doable by design.

```zig
pub something(data: []u8) void {
    data[0] = 1;
}

```

Say for example I could use some kind of “in situ” syntax:

```zig
something(mutable &[1]u8{ 0 }); 

```

Well… now what? How do I get my array back? That function returns `void` so I just kinda threw it down a hole. This may be because the example you’re posting is a simplified one but in this case, it’s really not understandable. Not trying to nitpick ya, but it’s hard to know what the goal is here.

* * *

Fundamentally, remember what a slice is - it’s a length and a pointer. Slices don’t contain stuff, they point to stuff and tell you how much stuff is there. So if I create a slice to something, it needs to be in a defined location.

Creating literals like in your example actually _does_ put it somewhere! It’s just in data segment of your binary. If you want to see this in action, please read this thread: [Diving deep into anonymous struct literals - #3 by bnl1](https://ziggit.dev/t/diving-deep-into-anonymous-struct-literals/2931/3)

So as it stands, as far as slices are concerned, I don’t think this really makes sense. Hopefully you get what I’m driving at here.

---

_[View the full topic](https://ziggit.dev/t/how-to-anonymously-initialize-a-slice/5687)._
