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.
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). The semantics of + is described as following: "The result of the binary + operator is the sum of the operands".
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.
Did you catch that?
Arrays are defined in terms of pointers (elsewhere)
, and pointers are defined in terms of arrays.
So yes, it's correct (
if we make some implicit assumptions (that does not hold true outside the C++world) about the standard). That does not make it better or worse.
----
iammisc wrote:@Shark8: I say you're statements are false because in many ways they are not criticizing C but rather your own personal opinions as to why C is bad
Which is what this thread was about. Not "why is C bad", but "why don't you use C". Read the first post. All the answers to the last question will obviously be opinions.
If you don't like it personally, it does not make it bad
Read the original question: "why don't you use C". "Because they way it works makes no sense to me" surely has to be a perfectly valid answer.
----
iammisc wrote:When it comes to pointer arithmetic, 4 + 1 is not 5 because a pointer is not a number(although it may be represented as one).
iammisc wrote:A pointer is an integer
It's an integer, but not a number?
----
in the context of a pointer to an integer, the next integer is not 1 byte away.
Since when did pointers get a concept of next?
----
Solar wrote:Shark8 wrote:Like assignments allowable in conditional tests...
That's one of the problems that stems not so much from the language, but from people not realizing that compiler warnings are something helpful, not a nuisance to be > /dev/null'ed.
It is my opinion that when you have these choices:
A. Make a language where it's hard to make a common mistake
B. Make a language where it's easy to make a common mistake, and instead give a warning about it, so that the user has to go back and correct it
it's obvious that you'd want the first one. B gives a lot of extra work to
both the compiler writer
and the programmer. It's usually a trade-off. In rare cases you might find a win-win situation, good for both the compiler writer and the programmer. Only C has repeatedly chosen solutions that time-consuming for both.
----
Ok, everyone, have a look at this:
Apparently a perfectly legal piece of C code. While it is "correct" that it does not compile (language authors decide what is correct), it is not intuitive that otherwise legal code fails due a brainless implementation of the preprocessor.
----
Colonel Kernel wrote:
I read somewhere that K&R never intended for C to be used without lint. I wonder if there's any truth to that.
If it isn't true, we just delete the last two words, right?