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.

The 5-minute Guide to C Pointers

110 pointsby denniskubesalmost 13 years ago

14 comments

Jun8almost 13 years ago
30 second guide to C pointers, from <i>Alice in Wonderland</i>:<p>`It's long,' said the Knight, `but very, very beautiful. Everybody that hears me sing it -- either it brings the tears into their eyes, or else -- '<p>`Or else what?' said Alice, for the Knight had made a sudden pause.<p>`Or else it doesn't, you know. The name of the song is called "Haddocks' Eyes."'<p>`Oh, that's the name of the song, is it?' Alice said, trying to feel interested.<p>`No, you don't understand,' the Knight said, looking a little vexed. `That's what the name is called. The name really is "The Aged Aged Man."'<p>`Then I ought to have said "That's what the song is called"?' Alice corrected herself.<p>`No, you oughtn't: that's quite another thing! The song is called "Ways and Means": but that's only what it's called, you know!'<p>`Well, what is the song, then?' said Alice, who was by this time completely bewildered.<p>`I was coming to that,' the Knight said. `The song really is "A-sitting On A Gate": and the tune's my own invention.'<p>The song that the Knight is referring to is this one: <a href="http://en.wikipedia.org/wiki/Haddocks%27_Eyes" rel="nofollow">http://en.wikipedia.org/wiki/Haddocks%27_Eyes</a>
评论 #4389951 未加载
rosseralmost 13 years ago
Every time someone tries offering a simplified explanation of pointers, I've countered with the old Buddhist saying that, "The pointing finger is not the moon," followed by a brief foray into syntax and operators, e.g.,<p><pre><code> moon* finger = &#38;luna; </code></pre> As often as not, enlightenment occurs.
评论 #4390088 未加载
评论 #4390995 未加载
sownalmost 13 years ago
Pointers are sometimes not arrays. :)<p><pre><code> extern int *x; extern int y[] </code></pre> x is a pointer to an int and y is a pointer to an array of ints of unspecified size. It's equivalent to saying float x and then extern int x somewhere else. They're type mismatched.<p>If you said 'x is a char pointer', you mean lookup symbol table for address of x, then do a memory address dereference and then get the contents of the memory at dereferenced address.<p>If you said 'x is a char array', you mean use the symbol table to get the address, then calculate offset and get contents from that address.<p>When you define it one way and then do it another, you end up doing both of those above. get contents of x, then get value of offset, add it to the contents of x, then get contents of the resulting address+offset.<p>The issue is the declaration which can happen many times and the definition which occurs just once.<p>Now, you can have an array and a pointer be equivalent if it is used in an expression because the compiler converts array references to pointers.
评论 #4391450 未加载
_kst_almost 13 years ago
&#62; Imagine an array variable like a pointer that cannot be changed that holds the memory address of the first element of the array it points to.<p>Nope. Arrays are not pointers; pointers are not arrays. This is perhaps the most common misconception about C, and a "Guide to C Pointers" should not propagate it.<p>For more information about why this is wrong, read section 6 of the [comp.lang.c FAQ](<a href="http://www.c-faq.com" rel="nofollow">http://www.c-faq.com</a>).
评论 #4390057 未加载
评论 #4390248 未加载
sausagefeetalmost 13 years ago
When printfing pointers you need to cast them to (void\<i>). %p only prints void\</i> and there is no guarantee two pointer types have the same size. And I can't figure out how to write an astrix.<p>I also think it is a mistake to bring up memory addresses so early. The first paragraph is incredibly confuses.<p>A pointer is simple. It's a variable that points at something. You can change where it points and you can change the value that it is pointing to. (&#38;) is the pointer-to operator and gives you a pointer to something.<p>I think that is roughly all that needs to be said. The all of locations, memory, addresses is just confusion.
评论 #4390275 未加载
manaskarekaralmost 13 years ago
Here's another one with more information on pointers in C.<p><a href="http://www.thegeekstuff.com/2012/01/advanced-c-pointers/" rel="nofollow">http://www.thegeekstuff.com/2012/01/advanced-c-pointers/</a>
评论 #4390315 未加载
phaoalmost 13 years ago
Yet another broken C pointers guide out there to confuse people.
评论 #4389876 未加载
评论 #4390545 未加载
pjmlpalmost 13 years ago
Nice tutorial!<p>Maybe it shows my age, but I am yet to understand why so many developers nowadays have such a hard time grasping pointers, regardless of the language being used to teach them.
评论 #4390222 未加载
评论 #4390298 未加载
评论 #4391464 未加载
bnegrevealmost 13 years ago
&#62; The * operator is used to both declare a pointer variable and to dereference a pointer depending on where it appears.<p>You seem to be suggesting that this is an inconsistency, it isn't:<p><pre><code> int *ptr;</code></pre> means that<p><pre><code> (*ptr) </code></pre> is of type ptr.<p><pre><code> *ptr = 3; </code></pre> Means the same and affect 3 to (*ptr)
评论 #4390573 未加载
ExpiredLinkalmost 13 years ago
I expected a repost of a repost of this: <a href="http://www.youtube.com/watch?v=f-pJlnpkLp0" rel="nofollow">http://www.youtube.com/watch?v=f-pJlnpkLp0</a>
khakimovalmost 13 years ago
thanks, also recommend to read "What do people find difficult about C pointers?" <a href="http://stackoverflow.com/questions/4025768/what-do-people-find-difficult-about-c-pointers" rel="nofollow">http://stackoverflow.com/questions/4025768/what-do-people-fi...</a>
评论 #4390122 未加载
chris_wotalmost 13 years ago
Nothing on pointer pointers :( so sad.
评论 #4394119 未加载
Kiroalmost 13 years ago
So... What do you use them for?
评论 #4390788 未加载
derlethalmost 13 years ago
The nontechnical guide to C pointers:<p><i>They're post office box numbers.</i><p>In this analogy, the post office is all your RAM. The big place where my analogy breaks down a bit is that, in this little world, the post office will store things bigger than one box in multiple adjacent boxes, so to get anything into or out of the post office you have to specify which box you want them to start with and how many boxes they'll have to use.<p>The star (dereference) operator takes as its argument a post office box number and instructs the machine to go to that post office box and start taking things out of enough boxes beginning with that box to satisfy the type of the pointer. The only things that only take one box are char values; the number of boxes everything else takes up depends greatly on the specific kind of hardware.<p>It's possible to put a slip of paper containing a number into a post office box; in 32-bit x86, a number long enough to encode a post office box number takes up four boxes. In 64-bit x86, it takes up eight boxes.<p>Using two stars means 'Go to this box, take out enough stuff from the next few boxes to make a pointer, go to the box specified by that pointer, and take enough stuff out from that box (and possibly the next few) to satisfy the type of that pointer.' Using three stars involves another go-to-box step, using four stars another, and using five stars is usually a sign of gross mental derangement.<p>(Oh, and if you're running under an OS much more featureful than MS-DOS, the post office box is a total lie told to your application by the OS. Getting into <i>that</i> gets a bit complicated.)