TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

So you think you know pointers?

62 pointsby niyazpkover 14 years ago

9 comments

drblastover 14 years ago
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.<p>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.<p>At least for me, no other languages including assembler seemed to generate this amount of confusion over such a simple thing.
评论 #1735770 未加载
评论 #1736585 未加载
kqr2over 14 years ago
Further clarification from the C FAQ:<p><a href="http://c-faq.com/aryptr/aryptrequiv.html" rel="nofollow">http://c-faq.com/aryptr/aryptrequiv.html</a><p><a href="http://c-faq.com/aryptr/aryvsadr.html" rel="nofollow">http://c-faq.com/aryptr/aryvsadr.html</a><p><a href="http://c-faq.com/aryptr/aryptr2.html" rel="nofollow">http://c-faq.com/aryptr/aryptr2.html</a>
jaimzobover 14 years ago
The Butt-Ugly Fish Book has a great section on this: <a href="http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298/" rel="nofollow">http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp...</a><p>In fact it's filled with great sections - go read it if you haven't.
pietrofmaggiover 14 years ago
If you enjoy this kind of puzzle "The C Puzzle Book" if full of them with a clear explanation: <a href="http://www.amazon.com/Puzzle-Book-Alan-R-Feuer/dp/0201604612" rel="nofollow">http://www.amazon.com/Puzzle-Book-Alan-R-Feuer/dp/0201604612</a><p>It's a funny little book that can keep you busy during compilation time.<p>And the "C Reference Manual" (<a href="http://www.careferencemanual.com/" rel="nofollow">http://www.careferencemanual.com/</a>) is the best, up-to-date reference to understand how this small language can be abused.
cafover 14 years ago
If it had declared<p><pre><code> struct { int x[4] } s; </code></pre> ...then &#38;s, &#38;s.x and s.x would all evaluate to the same address, but with three different types.
d0mover 14 years ago
Damn, I failed the last one.. thinking it would only have been a pointer further.
Robin_Messageover 14 years ago
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.
cybernytrixover 14 years ago
gaaaaaa this made it to HN's frontpage?
CamperBobover 14 years ago
printf("%p\n", (void*) (&#38;x + 1));<p>News flash: it's easy to confuse people such as myself who don't know, and don't care, whether the &#38; 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.
评论 #1735079 未加载
评论 #1735217 未加载
评论 #1735355 未加载
评论 #1735005 未加载