On Monday, 5 December 2022 at 15:08:41 UTC, rempas wrote:
Got it! I guess they could also just allow us to use bracket notation to do the same thing. So something like:

```d
foreach (i; 0 .. list.length) {
  (cast(int*)ptr[i]) = i;
}
```

This is what happens with arrays anyways. And arrays ARE pointers to a contiguous memory block anyways so they could do the same with regular pointers. The example also looks more readable.

You can use bracket notation with pointers. You just need to move your closing parenthesis a bit.

Assuming that `ptr` is a `void*`, these are all equivalent:

```d
(cast(int*) ptr)[i] = whatever;
*((cast(int*) ptr) + i) = whatever;
*(cast(int*) (ptr + i * int.sizeof)) = whatever;
```

Reply via email to