News for hackers. Stuff that matters.
Re:Conflating pointers with arrays: C's biggest mistake? (2009) by jkabrg on Sunday July 22, 2018 @05:25AM (#17585985)
WalterBright posted the following code snippet somewhere above:

     char foo[9]; foo[] = "abc"; foo[3..3+5] = "1234";
`foo` is an array. `foo[3..8]` is a slice, which is an object that does its own bounds-checking. I don't think the heap is used here.

Another explicit example:

  char foo[5] = "abcd"; // still NUL-terminated, carries length aswell
  char[] bar = foo[2..3]; // a fat pointer with length 1
  bar[0] = 'C';
  printf(foo); // abCd
  bar[1] = 'D'// ERROR!!! bar has length 1
Note that the array `foo` is now bounds-checked, which may affect backwards-compatibility. Also, `bar` is no longer null-terminated, which means you can't do printf on it.

There are no comments on this comment yet.