Page 1 of 1

[NO REPLY] Iterators (rev #2, 2018-1-15)

Posted: Sun Dec 10, 2017 6:06 pm
by Giovanni
It is easy to get indexes wrong while scanning an array so a new keywork is required: foreach.

Code: Select all

uint32_t array[16];

foreach (p in array) {
  *p = (uint32_t)0
}

foreach (p in "Hello World!!") {
  put(*p);
}


No pointer declaration needed, no need to know the size of the array, no need to use macros for the array size and other tricks, no confusion between 0 and 1 as base and < or <= as relation.

  • Iterating on a scalar type is like iterating on an array of size 1.
  • The pointer is implicitly a pointer to the array element type but constant, it is not modifiable.
  • Element index can be obtained using pointer arithmetic (p - array).

Giovanni