Craze Frog wrote:Solar wrote:
1) Pointers behave the way they do because the semantics of "++ptr" is "point to the next element". If ptr points to an integer, that means "point to the next integer". Not the second half of the current one, the next one. There is nothing "wrong" or "right" about this behaviour compared to what you would prefer, it's just the way C works. It ain't "broken", it's just not what you liked, so it's a matter of dislike, not breakage.
While you didn't reply to me, I must say that it is
indeed not breakage, but that does not make it less horrible.
Apart from that, the semantics of the increment and decrement operators of C++ are very clearly described in the C++ ANSI standard. Sorry to disappoint you, but here it is: "The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated)."
It does not mention special cases for pointers. So what you said is actually wrong. There are no such semantics defined for that operator.
You're right, the semantics are clearly defined, so why are you misrepresenting it? Quoting from the C++ standard:
C++ standard, 5.3.2 wrote:The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated).
The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer
to a completely-defined object type. The value is the new value of the operand; it is an lvalue. If x is not
of type bool, the expression ++x is equivalent to x+=1. Note: see the discussions of addition (5.7) and
assignment operators (5.17) for information on conversions.
This quite clearly tells us that ++ is is equivalent to using the addition operator, so even though there's no special casing for pointers here (and on that note, no special casing for any other type apart from bool) we have to check the sections on addition and assignment.
Craze Frog wrote:So 4+1 = 8 must come from the binary + operator (we assume they use the semantics of + when they said "adding", because else using ++ for pointer arithmetic would be a violation of the standard).
Why assume anything? It says right there. And didn't you say that the semantics were well defined?
Craze Frog wrote:The semantics of + is described as following: "The result of the binary + operator is the sum of the operands".
Did you completely miss the rest of the section describing semantics for +/- on pointers? I'd quote it, but it's huge
Craze Frog wrote:And after that, they contract themselves by making an exception for arrays, and defining non-array pointers to behave like they point to the first element of an array with 1 element.
I guess you didn't and just ignored bits of it. This is not a contradiction, it's defining additional behaviour for an operator under certain conditions (for example: while and do...while, while is used both times, but behaves differently in a well-defined way. Hardly a contradiction)
Craze Frog wrote:Did you catch that? Arrays are defined in terms of pointers (elsewhere), and pointers are defined in terms of arrays.
Hmm, no they're not.
C++ standard, 5.7 wrote:For the purposes of these operators, a pointer to a nonarray object behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.
They behave the same way "for the purposes of these operators", a bit different than the way you seem to be representing it.