62 points · by niyazpk · 16y ago · 37 comments
I think the C++ programming book I first used made it worse by equating pointers and arrays from the start, making it seem like they were interchangeable.
At least for me, no other languages including assembler seemed to generate this amount of confusion over such a simple thing.
The semantics associated with accessing those elements in C is anything but, especially for a beginner.
http://c-faq.com/aryptr/aryptrequiv.html
In fact it's filled with great sections - go read it if you haven't.
It's a funny little book that can keep you busy during compilation time.
And the "C Reference Manual" (http://www.careferencemanual.com/) is the best, up-to-date reference to understand how this small language can be abused.
struct { int x[4] } s;
...then &s, &s.x and s.x would all evaluate to the same address, but with three different types.News flash: it's easy to confuse people such as myself who don't know, and don't care, whether the & unary operator is above or below the + binary operator in the precedence hierarchy. Anyone who writes an expression like this without parentheses has been educated beyond their wisdom. Ric has more at 11.
Me, I looked it up. He only said not to compile it, not that you couldn't learn something at the same time.
As usual you marginally write code for the compiler (or to impress you boss on how well you understand the C standard). The Very Important Reader of the code is the guy who fave to read it in the comings years to improve/fix something (and usually is the same soul who originally wrote the stuff).
So make a favor to yourself and write code you'll enjoying read at loony nights (as usual happens when something don't works).
and any C/C++ programmer who have spent few days with the language will never be confused by that
see http://www.cplusplus.com/doc/tutorial/pointers/
---- This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". ----
If (&x) returns "the address of x," you'd be inclined to say that ((&x) + 1) is one byte higher than &x, even though the C compiler will calculate the actual answer based on the size of x.
If x is an array as in this example, you'd have to know that C does this arithmetic based on the entire length of the statically defined array, rather than a single element as it would for a dynamically defined array with a pointer to it.
This is completely non-obvious, at least to me.
but when you see something tagged as 'C', 'pointers', 'challenge' I guess you can fairly assume it is an "advanced subject" for people already experienced in C no ?
Not to defend that guy or anything.
In case you mean exponentiation, these expressions would always be equal regardless of the precedence of the operators.
/rolls eyes
i.2+$i.3
0 1 2 3 4
That's "i. 2 + $ i. 3" (iota 2 plus shape_of iota 3), which parses as i.(2 + $(i. 3)). iota returns an array from 0 to n, + adds, shape_of returns the dimensions of a array/matrix/n-dimensional matrix/etc. + is a binary operator ("dyad"), i. and $ are unary operators ("monads", no relation to Haskell).