My approach to teaching "pointer arithmetic" #cplusplus #academia 
(assuming teaching this topic is appropriate, which is a different conversation)
Instructors and professionals: my slides on this topic are available for review and adaptation. Please DM.

(assuming teaching this topic is appropriate, which is a different conversation)
Instructors and professionals: my slides on this topic are available for review and adaptation. Please DM.
1. Use the term "address arithmetic" instead of "pointer arithmetic". The former creates a better mental image and aids comprehension #cplusplus
2. "Pointer arithmetic" creates an image of pointer variables being offset, etc, and is too abstract. #cplusplus
"Address arithmetic" creates an image of sequence of memory locations and moving up/down the sequence, which is not as abstract
"Address arithmetic" creates an image of sequence of memory locations and moving up/down the sequence, which is not as abstract
3. Emphasize that address arithmetic is to be performed only in the context of arrays, and only in the context of the same array. #cplusplus
Easier to teach adr arithmetic w/out arrays. Discuss array-ptr connection and how [] uses address offset after introducing adr arithmetic
Easier to teach adr arithmetic w/out arrays. Discuss array-ptr connection and how [] uses address offset after introducing adr arithmetic
4. Start by teaching address difference instead of starting with address offset. #cplusplus
Easy to see why the difference between addresses is an offset/distance (and why it can be negative), which makes it easy to see why offsetting an address gives another address
Easy to see why the difference between addresses is an offset/distance (and why it can be negative), which makes it easy to see why offsetting an address gives another address
5. Use char* to introduce both address difference and address offset. Obvious next step is to use short*, int*, float*,… #cplusplus
I find the question "how many chars/ints/floats are after address a1 up to and including a2?" to be very helpful to learning.
I find the question "how many chars/ints/floats are after address a1 up to and including a2?" to be very helpful to learning.
6. *Gently* introduce the result of address-difference to be of type std::ptrdiff_t. Then it is easy to see why the type of address offset is also std::ptrdiff_t. #cplusplus
7. If course is for "mature students" (upper-level course?), use "address algebra" to discuss the how and why of types: T* – T* → D and T* + D → T*. #cplusplus
The discussion is uncommon. I include parts of this formulation in ungraded exercise so those interested approach me
The discussion is uncommon. I include parts of this formulation in ungraded exercise so those interested approach me
8. Introduce array-pointer relationship and show how array subscript operation uses address offset. #cplusplus
Recommend using [] to access array elements and let the compiler do address arithmetic. True even if working w dynamic arrays
Related tweet: https://twitter.com/smurthys/status/1297285344870780929?s=20
Recommend using [] to access array elements and let the compiler do address arithmetic. True even if working w dynamic arrays
Related tweet: https://twitter.com/smurthys/status/1297285344870780929?s=20
9. Discuss why type of index variable used with subscript operator should be std::ptrdiff_t but (for historical reasons) it tends to be std::size_t. #cplusplus
I cite C++ Core Guidelines wherever relevant, but I avoid discussing gsl:index to prevent confusion in students.
I cite C++ Core Guidelines wherever relevant, but I avoid discussing gsl:index to prevent confusion in students.
10. Discuss why std::size_t is OK as type of index variable to access array elements with subscript operator. #cplusplus
If discussion not affordable, instruct students that variables used as array index or size should be std::size_t; not int or unsigned.
If discussion not affordable, instruct students that variables used as array index or size should be std::size_t; not int or unsigned.
11. Depending on course objectives, it might be easiest to just instruct to use std::size_t, but important to at least introduce the difference between std::size_t and std::ptrdiff_t
12. If course for "mature students", discuss the differences between std::size_t and std::ptrdiff_t, specifically ranges, overlaps, etc. #cplusplus
For any array size, address1 - address2 should be in range of std::ptrdiff_t. Demo this in GCC and MSVC https://godbolt.org/z/3W9ofG
For any array size, address1 - address2 should be in range of std::ptrdiff_t. Demo this in GCC and MSVC https://godbolt.org/z/3W9ofG
13. Obviously, there are many subtleties not covered in this thread, and every course, class, and instructor is different.
I welcome feedback and offer my slides for both critique and adaptation.
End.
I welcome feedback and offer my slides for both critique and adaptation.
End.