Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multidimensional Arrays #2894

Closed
mosullivan opened this issue Jul 14, 2019 · 6 comments
Closed

Multidimensional Arrays #2894

mosullivan opened this issue Jul 14, 2019 · 6 comments
Labels
Milestone

Comments

@mosullivan
Copy link

I looked through the documentation etc. but I can't seem to find any reference on how to define and initialise multidimensional arrays (e.g. arrays of strings). Is this supported and, if so, how would I do this?

@JohnathanFL
Copy link
Contributor

JohnathanFL commented Jul 14, 2019

Just keep reading each [] as "array of" and you already have it.

var arStr: [2][]const u8 = [_][]const u8{"Hello", "There"};

for(arStr) |str| {
    std.debug.warn("{}\n", str);
}
Hello
There

The initialization syntax ought to get somewhat better when we get this merged in.

@mosullivan
Copy link
Author

mosullivan commented Jul 14, 2019

Apologies if this isn't the correct place, but how would you pass this to a C function that takes the equivalent multi dimensional C-array?

For example:

var nameCount: usize = 4; //for example
var names: [*c][*c]const u8 = undefined;   
c_function(nameCount, &names);  // a c function that fills a const char** with data

@JohnathanFL
Copy link
Contributor

JohnathanFL commented Jul 14, 2019

I'm honestly not sure if there's a better place either.

This seems to compile at least:

// I believe the C function would be translated to something like this by zig
pub extern fn c_function(count: usize, data: [*c][*c]const u8) void;
...
var nameCount: usize = 4; //for example
var names: [4][]const u8 = undefined; 
c_function(nameCount, @ptrCast([*c][*c]const u8, &names));

If I'm understanding the casts correctly here, &names gets implicitly cast from *[N]T to [*]T, which is then allowed to be cast to a C pointer.

@mosullivan
Copy link
Author

Ah, I must have missed that in the casts section. Thank you :)

@andrewrk
Copy link
Member

[]T is a slice, not an array. A slice is a pointer and a length. A multi dimensional array looks like [N][M]T.

I'll leave this issue open to add a multidimensional array example to the docs.

@Gaai
Copy link

Gaai commented Nov 23, 2022

    var array1:  [8]u8       = .{ 1,2,3,4,5,6,7,8 };
    var array2d: [2][4]u8    = .{ .{1,2,3,4}, .{5,6,7,8} };
    var array3d: [2][2][2]u8 = .{ .{ .{1,2}, .{3,4} }, .{ .{5,6}, .{7,8} } };

Works and is kinda nice imho if know the lengths. Fits better with the other type definitions. Documentations has it like this:

const mat4x4 = [4][4]f32{
    [_]f32{ 1.0, 0.0, 0.0, 0.0 },
    [_]f32{ 0.0, 1.0, 0.0, 1.0 },
    [_]f32{ 0.0, 0.0, 1.0, 0.0 },
    [_]f32{ 0.0, 0.0, 0.0, 1.0 },
};

Which is overly verbose if you ask me... But gives you the option to infer the length, although in the example it is given anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants