Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on How do I (compactly) initialize a 2-d array of structs with the same value?
Post
How do I (compactly) initialize a 2-d array of structs with the same value?
+2
−0
I learned about the **
operator for array-repeat, and I love it.
I have a struct that contains a 2-d array of inner structs, like
const D1 = 30;
const D2 = 40;
const Inner = struct { .a: u32, .b: bool };
const Outer = struct {
// ...
.array: [D1][D2]Inner,
// ...
};
I would like to "just initialize" the struct in the init()
function:
pub fn init() Self {
return Self{
.array = FIXME("What goes here?"),
};
}
For a 1-d array, I could do something like:
// See line 63 of
// https://ziglang.org/documentation/master/#Arrays
.array_1d = [_]Inner{.a='1', .b=true} ** D2,
But how do I write the expression for a 2-d array?
.array = [_][_]Inner{...} ** D2 ** D1, // didn't work
1 comment thread