# How to initialize an "empty" sentinel terminated array?

**URL:** <https://ziggit.dev/t/how-to-initialize-an-empty-sentinel-terminated-array/2379>\
**Category:** Help\
**Tags:** language\
**Created:** [December 5, 2023, 4:49pm UTC](https://ziggit.dev/t/how-to-initialize-an-empty-sentinel-terminated-array/2379 "2023-12-05T16:49:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustafla](https://ziggit.dev/user_avatar/ziggit.dev/gustafla/32/351_2.png) [@gustafla](https://ziggit.dev/u/gustafla)\
**Post date:** [December 5, 2023, 4:49pm UTC](https://ziggit.dev/t/how-to-initialize-an-empty-sentinel-terminated-array/2379/1 "2023-12-05T16:49:56Z")

</div>

Hi. I’m trying to use a sentinel-terminated array as a stack or such. I want to do something like this:

```zig
var list: [20:0xffff]u16 = .{}; // sentinel-terminated array with 0xffff in index 0

```

That does not work, the compiler error is `error: expected 20 array elements; found 0`, which makes sense.

But then if I do this:

```zig
const print = @import("std").debug.print;

pub fn main() !void {
    var list: [20:0xffff]u16 = .{0xffff} ** 20;
    print("Len {}, ", .{list.len});

    list[0] = 1;

    for (list) |x| {
        print("{x} ", .{x});
    }
}

```

The program prints  
`Len 20, 1 ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff`.  
This makes little sense, because Zig documentation states that

> The syntax `[N:x]T` describes an array which has a sentinel element of value `x` at the index corresponding to the length `N`.

---

<div class="post-metadata">

**Author:** ![dee0xeed](https://ziggit.dev/letter_avatar_proxy/v4/letter/d/3ab097/32.png) [@dee0xeed](https://ziggit.dev/u/dee0xeed)\
**Post date:** [December 5, 2023, 4:58pm UTC](https://ziggit.dev/t/how-to-initialize-an-empty-sentinel-terminated-array/2379/2 "2023-12-05T16:58:16Z")

</div>

> [@gustafla](#):
>
> `var list: [20:0xffff]u16 = .{0xffff} ** 20;`

`var list: [20:0xffff]u16 = .{0x0000} ** 20;` ?

---

<div class="post-metadata">

**Author:** ![gustafla](https://ziggit.dev/user_avatar/ziggit.dev/gustafla/32/351_2.png) [@gustafla](https://ziggit.dev/u/gustafla)\
**Post date:** [December 5, 2023, 5:01pm UTC](https://ziggit.dev/t/how-to-initialize-an-empty-sentinel-terminated-array/2379/3 "2023-12-05T17:01:46Z")

</div>

I think I have some misconception about these sentinel terminated arrays.  
The documentation also states that

```zig
    // The sentinel value may appear earlier, but does not influence the compile-time 'len'.
    const array = [_:0]u8 {1, 0, 0, 4};

    try expect(@TypeOf(array) == [4:0]u8);
    try expect(array.len == 4);
    try expect(array[4] == 0);

```

So it’s not really usable for what I want to do with it anyway.
