# Initialization of a field versus a decl of type \[\_\]T

**URL:** <https://ziggit.dev/t/initialization-of-a-field-versus-a-decl-of-type-t/3964>\
**Category:** Help\
**Tags:** language\
**Created:** [April 15, 2024, 3:22pm UTC](https://ziggit.dev/t/initialization-of-a-field-versus-a-decl-of-type-t/3964 "2024-04-15T15:22:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [April 15, 2024, 3:22pm UTC](https://ziggit.dev/t/initialization-of-a-field-versus-a-decl-of-type-t/3964/1 "2024-04-15T15:22:43Z")

</div>

here’s an array-valued `const` of type `[_]T` which i declare as follows:

```zig
const table = [_]T { v1, v2, ... };

```

now, i want to define a `struct` type that has a similarly named field:

```zig
const S = struct {
    table: [_]T,
       ...
};

```

i then attempt to declare a `const` of type `S` as follows:

```zig
const s = S{
    .table = [_]T { v1, v2, ... },
        ...
};

```

but i receive compiler errors… what’s the correct way to express this initialization???

a related question is assigning a default value to the `.table` field… how do i express a zero-length array of type `[_]T` ???&nbsp; alternatively, should i declare the type as `?[_]T` and then use `null` as the default???

---

<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:** [April 15, 2024, 3:38pm UTC](https://ziggit.dev/t/initialization-of-a-field-versus-a-decl-of-type-t/3964/2 "2024-04-15T15:38:58Z")

</div>

The issue here is that `[_]T` doesn’t have a length defined - it’s deduced based on the number of elements you declare it with. It’s an odd bit of syntax because the type doesn’t evaluate to `[_]` but instead becomes an array with a length when it’s deduced:

```zig
pub fn foo(_: [5]usize) usize {
    return 5;
}
export fn bar(_: i32) usize {
    const array = [_]usize{ 1, 2, 3 };
    return foo(array);
}

```

Returns the error:

```zig
example.zig:9:16: error: expected type '[5]usize', found '[3]usize'

```

So we can see that syntax doesn’t survive the assignment operation - it becomes `[3]usize`.

One option here is to take in a length parameter when you create this array:

```zig
pub fn foo(comptime T: type, comptime N: usize) type {
    return struct {
        table: [N]T,
        ...
    };
}

```

Then you could get:

```zig
const s = foo(i32, 3){
    .table = [_] i32 { 0, 1, 2 },
};

```

If you don’t know the length of the item and you want to keep it in a struct without having to pass a size parameter, your most direction option is to declare it as a slice `[]const T`.

---

<div class="post-metadata">

**Author:** ![biosbob](https://ziggit.dev/user_avatar/ziggit.dev/biosbob/32/1987_2.png) [@biosbob](https://ziggit.dev/u/biosbob)\
**Post date:** [April 15, 2024, 3:48pm UTC](https://ziggit.dev/t/initialization-of-a-field-versus-a-decl-of-type-t/3964/3 "2024-04-15T15:48:11Z")

</div>

of course!!! this is the same problem as having array-valued fields in C struct (whose size must known at compile time)…

the slice type worked great… and i can use `&.{}` as its default value…
