One of the most challenging concepts to instill in new CS students is 0-indexing (indeed, the pedagogy of this fact probably merits its own discussion). Another difficult topic -- although a slightly more advanced one -- is pointers. (I'm thinking particularly of programming in C.) With this question I'm wondering if part of the difficulty is the syntactic sugar in C that allows the following to be equivalent:
// declare array of 5 ints on the stack int num; // use array notation to change first element num = 42; // use pointer arithmetic to change first element *(num + 0) = 42;
With array notation, we are explicit with 0-indexing, but the logic of it isn't apparent. Yet, with pointer arithmetic, it's more clear (I think...) why we use 0: the pointer stores the base address, so dereferencing the pointer brings us to that address which is where the array logically begins.
Comfort with this leads to the topic of memory management on the heap with something like this:
// declare array of 5 ints on the heap int *num = malloc(sizeof(int) * 5); // use pointer arithmetic to change first element *num = 42;
I am toying with expanding my introduction on arrays next year to include this particular use of pointers. I recognize that it would involve taking roughly a single lesson on arrays and expanding to something closer to a week-long series to tie all of these ideas together.
However, is it worth putting aside the syntactic sugar in order to understand more accurately the indexing of arrays? On the other hand, does the introduction of pointers and memory complicate the process so much so that confusion about arrays will increase rather than decrease? (I'm thinking this is a "lesson idea feedback" discussion.)