Struct with array as field

Hi,

(using zig HEAD directly compiled from repo on Linux)

the following code:

const std = @import(“std”);
const mem = @import(“std”).mem;
const print = std.debug.print;

const EnigmaD = struct {
etw:[]u8=“QWERTZUIOASDFGHJKPYXCVBNML”
};

pub fn main() !void {

const x=EnigmaD;

print(“—{s}—\n”,x.etw[3]);

}

fails with

error: struct ‘main.EnigmaD’ has no member named ‘etw’
print(“—{s}—\n”,x.etw[3]);

What did I wrong here, why does this fail?

Cheers!
Tuxic

I think x in your code is not an instance of a struct, it’s a type.

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

const EnigmaD = struct {
    etw: []const u8 = "QWERTZUIOASDFGHJKPYXCVBNML",
};

pub fn main() !void {
    const x = EnigmaD{};
    print("—{c}—\n", .{x.etw[3]});
}
$ ./1 
—R—
1 Like

BTW, you have wrong double quotes in the code.
Compare

const std = @import(“std”); // wrong, 0xE2 0x80 0x9C
const std = @import("std"); // corrrect, 0x22
1 Like

Hi dee0xeed ( your online name puts a big smile onto my face…GREAT idea!!! )

Yeah! THAT has helped me a lot! :slight_smile: :slight_smile: :slight_smile:

Next challenge:
The rotor configuration is now in place…the configuration of the so called “turover points” (where the next wheel will be advance) is missing still.

Instead of the one dimensional slice I need a two dimensional one.
At least I am a master creator of syntax errors… :slight_smile:

One thing which confuses me currently:
When do I have to use “:” and when do I need “=” in the creation of variables and types in a broadest sense of these words?

Cheers!
Tuxic!

BTW2, etw: []const u8 is not an array, it is a slice (pointer + length)