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

A place of insane ideas, nothing to see here.
User avatar
Giovanni
Site Admin
Posts: 10739
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 306 times
Been thanked: 257 times
Contact:

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

Postby Giovanni » Sun Dec 10, 2017 6:06 pm

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

Return to “Safer C”

Who is online

Users browsing this forum: No registered users and 1 guest