On Tue, Aug 25, 2020 at 5:34 PM Bruce Vining <bruce.vining@xxxxxxxxx> wrote:
The one language I really can't
stand, and it's just heritage I think, is C. If I add 1 to a pointer I am
anticipating addressing the next byte of storage, not the first byte of the
data type the pointer is associated with.
In case people are trying to follow along, I think a better way to
express that last part is:
In C, pointers and arrays are designed to be (basically) equivalent.
So if you increment a "pointer" by one, you are actually accessing the
next "element" of the array, which means you've advanced the pointer
by the *size of* the data type it's associated with.
And, whether or not that matches your intuition, of course it's just
the compiler trying to be helpful. After all, if you've declared a
pointer to 4-byte integers, it is vastly, vastly, vastly more likely
that you want to get the next 4-byte integer than that you want to get
the 2nd byte within a 4-byte integer. Further, if you've declared a
pointer to a 4-byte integer and you increment that memory address by
one byte, then if you try to de-reference the pointer at that point,
you'll be reading the 2nd, 3rd, and 4th byte of the first integer, and
the 1st byte of the next (and interpreting those 4 bytes you just read
as a 4-byte integer).
It's true that C has a comparatively fast and loose notion of types,
but it does have *some* notion of types! It's not all just bytes!
John Y.
As an Amazon Associate we earn from qualifying purchases.