#Arrays in Zig

1 messages · Page 1 of 1 (latest)

fiery falcon
#

Hello im VERY new to Zig, and im a bit confused about arrays, for example lets say we have this:
var confusedArr:[10]i32 = [_]i32{1} ** 10
should i understand this as

  1. in the right side we are actually creating the array we want. But in two steps, first we create an arr of size 1 then we do as the docs call it an array multiplication to get an array of size 10.
  2. Then in the left side "as my C brain told me" we are pointing to the array that was created with the confused var 🙂
    is that correct ?
    Also if you guys have any examples/tutorials related to arrays or Zig in general i would appretiate them.
long dome
#

1 is correct
2 is incorrect

Arrays in zig aren’t pointers

lucid drum
#

Yeah, in C arrays degrade to pointers but in zig it's more like literally that number of elements. I probably can't describe in c terms as well as other people here but think of it kindof like

struct _MyNewArray {
  int e1;
  int e2;
  //...
} confused_arr;

var confused_arr: [10]i32 is declaring confused_arr with a type of [10]i32

and this other part

= [_]i32{1} ** 10 is kinda weird. But essentially you're saying array of size 1 with value 1 repeated 10 times.

nova fog
#

Another way to write the array value is @splat(1), which makes an array of the type and length of the place you assign it to.

fiery falcon
nova fog
#

You'll be hearing a lot about result locations in zig.

fiery falcon
#

but i think what @lucid drum said made it a bit more understandable to me thank you all

karmic steeple
#

array multiplication is a bad name for this operator, its array repetition

fiery falcon
#

yep that might need a little update in the docs

lucid drum