
So you think you know pointers? - niyazpk
http://stackoverflow.com/q/232303/184
======
drblast
I wonder if pointers confuse people because C's syntax for pointers is
confusing. The confusing examples always involve arrays, where a statically
allocated array's behavior is different from one that's dynamically allocated
and not necessarily what you would expect as a beginner, or where the vagaries
of operator precedence obfuscate the pointer arithmetic.

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.

~~~
cynicalkane
C style arrays are the lowest level model for flat arrays that I can imagine.
Maybe it's more accurate to say that computers are confusing, and C is just
being C.

~~~
drblast
But computers aren't confusing; the idea of a block of N bytes memory
containing M-byte sized elements is dead-simple, and the same whether it's
statically or dynamically allocated.

The semantics associated with accessing those elements in C is anything but,
especially for a beginner.

~~~
cynicalkane
Arrays are "dead simple", but addressing their elements can get very
complicated, especially if you might want to address the array itself.

~~~
shadowfox
Can you explain?

------
kqr2
Further clarification from the C FAQ:

<http://c-faq.com/aryptr/aryptrequiv.html>

<http://c-faq.com/aryptr/aryvsadr.html>

<http://c-faq.com/aryptr/aryptr2.html>

------
jaimzob
The Butt-Ugly Fish Book has a great section on this:
[http://www.amazon.com/Expert-Programming-Peter-van-
Linden/dp...](http://www.amazon.com/Expert-Programming-Peter-van-
Linden/dp/0131774298/)

In fact it's filled with great sections - go read it if you haven't.

------
pietrofmaggi
If you enjoy this kind of puzzle "The C Puzzle Book" if full of them with a
clear explanation: <http://www.amazon.com/Puzzle-Book-Alan-R-
Feuer/dp/0201604612>

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.

------
caf
If it had declared

    
    
         struct { int x[4] } s;
    

...then &s, &s.x and s.x would all evaluate to the same address, but with
three different types.

------
d0m
Damn, I failed the last one.. thinking it would only have been a pointer
further.

------
Robin_Message
That seems like an odd feature. I mean, useful in some cases where the size of
the array is statically determined, but kind of fiddly and not the same across
such unusual things as function calls.

------
cybernytrix
gaaaaaa this made it to HN's frontpage?

------
CamperBob
printf("%p\n", (void*) (&x + 1));

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.

~~~
psyklic
Can you provide an example, from any language, where a unary operator has a
lower precedence than a binary operator?

~~~
ryanf
-3^5 = -(3^5)

Not to defend that guy or anything.

~~~
CamperBob
Yeah, because it's impossible to be a skilled C programmer without knowing
this stuff by heart.

/rolls eyes

~~~
ryanf
There are plenty of confusing order-of-operation things in C, especially
around pointers, but is "&x + 1" really one of them? I basically know nothing
about this subject and it's still pretty clear that the & would take
precedence. Would you balk at that expression if x were an int pointer?

~~~
CamperBob
This particular case isn't that interesting because if the expression _were_
equivalent to &(x+1) the compiler would complain about taking the address of a
non-lvalue.

But ryanf, while not defending "that guy" (me), made my point for me: if I
have to stop and think about whether -3^5 == -(3^5) because some hotshot
didn't consider it manly to use parentheses, nobody wins.

If I were writing that expression I'd use (yes, completely unnecessary)
parentheses around &a out of habit... the same habit that wouldn't let me
write -3^5. Either way, it doesn't burn any more cycles.

