Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure the audience is a bunch of Java programmers learning C and C++.
Does anyone have any good articles or explanations (blogs, examples) for pointer arithmetic? Figure
Share
First, the binky video may help. It’s a nice video about pointers. For arithmetic, here is an example:
(Note that incrementing a pointer that contains a null pointer value strictly is undefined behavior. We used NULL because we were only interested in the value of the pointer. Normally, only use increment/decrement when pointing to elements of an array).
The following shows two important concepts
On a practical example. Suppose you write a function and people provide you with an start and end pointer (very common thing in C++):
ptrdiff_tis what is the type of (end – begin). It may be a synonym for ‘int’ for some compiler, but may be another type for another one. One cannot know, so one chooses the generic typedefptrdiff_t.