pointer increment
Maarten Litmaath
maart at cs.vu.nl
Tue Aug 22 04:16:27 AEST 1989
wolfgang at ruso.UUCP (Wolfgang Deifel) writes:
\maart at cs.vu.nl (Maarten Litmaath) writes:
\> I wrote:
\>\...
\>\ ((char*)ptr)++ ;
\>\Now ptr becomes 1 ( = sizeof(char) ) more.
\
\>Aaaaaaarrrrrgghh! Wayne Throop's Sacred Mission isn't finished yet...
\ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
\
\ Thank god that you are born as expert...
There's no need to feel p*ssed off, Wolfgang; I wasn't trying to insult you
or something.
Of course I should have added a detailed explanation, but I figured someone
else would supply that.
Having read your article I remembered Wayne Throop's 1985 (1986?) crusade
against "the vile heresy which claimeth that ((char*)ptr)++ will increment
ptr" (free after Henry Spencer's Ten Commandments for C Programmers).
Allright. A compiler that allows the abovementioned construct is wrong for
two reasons:
1) It allows the `++' operator to be applied to an Rvalue expression;
only Lvalue expressions may be operand of an increment operator, e.g.
x++
a[i]++
This isn't so strange:
x++
is equivalent to
x = x + 1
which doesn't make sense for arbitrary (Rvalue) expressions.
2) It increments the wrong variable; a cast is equivalent to an
assignment to an invisible temporary variable (with the usual
restrictions and conversions):
foo x;
... (bar) x ...
becomes
foo x;
bar cast_tmp; /* `invisible' temp variable */
... (cast_tmp = x) ...
If we write the latter expression as
(cast_tmp = x, cast_tmp)
it's clearly `cast_tmp' which should be incremented (if a `++'
operator is appended), were this possible at all; definitely NOT `x'.
--
"rot H - dD/dt = J, div D = rho, div B = 0, |Maarten Litmaath @ VU Amsterdam:
rot E + dB/dt = 0" and there was light. |maart at cs.vu.nl, mcvax!botter!maart
More information about the Comp.lang.c
mailing list