Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In your own studies (on your own, or for a class) did you have an "ah ha" moment when you finally, really understood pointers? Do you have an explanation you use for beginner programmers that seems particularly effective?

For example, when beginners first encounter pointers in C, they might just add &s and *s until it compiles (as I myself once did). Maybe it was a picture, or a really well motivated example, that made pointers "click" for you or your student. What was it, and what did you try before that didn't seem to work? Were any topics prerequisites (e.g. structs, or arrays)?

In other words, what was necessary to understand the meaning of &s and *, when you could use them with confidence? Learning the syntax and terminology or the use cases isn't enough, at some point the idea needs to be internalized.


Update: I really like the answers so far; please keep them coming. There are a lot of great perspectives here, but I think many are good explanations/slogans for ourselves after we've internalized the concept. I'm looking for the detailed contexts and circumstances when it dawned on you.

For example:

I only somewhat understood pointers syntactically in C. I heard two of my friends explaining pointers to another friend, who asked why a struct was passed with a pointer. The first friend talked about how it needed to be referenced and modified, but it was just a short comment from the other friend where it hit me: "It's also more efficient." Passing 4 bytes instead of 16 bytes was the final conceptual shift I needed.

share|improve this question

closed as too broad by MichaelT, Bart van Ingen Schenau, gnat, jwenting, Dan Pichelman Aug 14 at 17:25

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

14  
'Shotgun' method: throw *s everywhere til it works. –  Michael K Nov 9 '10 at 15:15
14  
@Michael: Yeah, good luck with that. –  Robert Harvey Nov 9 '10 at 15:44
8  
This question comes up shortly after at the start of every semester on SO. The latest incarnation is here: stackoverflow.com/questions/4118647/… –  Tim Post Nov 9 '10 at 16:22
10  
What's a nice explanation for pointers? Brian Kernhigan and Dennis Ritchie hate software engineers. –  Adam Crossland Nov 9 '10 at 16:51
3  
It "clicked" for me after having spent some time single-stepping machine code in the debugger. Yes, I was familiar with basics of computer architecture -- I learned assembly on Commodore 64, and x86 ASM was pretty straightforward after that. So I already knew the concept of an "address", I just didn't understand how it mapped to C's syntactical constructs. –  zvrba Nov 10 '10 at 11:27

32 Answers 32

Assuming you already understand arrays at least at a superficial level, my best explanation for pointers would be this:

Imagine all of the memory in your computer (or address space in your process if you want to be more precise) as one giant array of bytes. A pointer is just an index into that array. Pointers are therefore just a special kind of integer.

Learning about all the tricks that C and assembly coders could do by treating pointers as integers, and how the distinction is purely convention at the assembly language level, helped solidify this understanding for me.

share|improve this answer

This document really helped me get it straight in my head: http://www.udel.edu/CIS/105/pconrad/03F/2003.fall.doc

I linked to the UD version because I like the extra explanation P. Conrad added, but the original Stanford version is just as good. All of these Stanford intro CS docs have a permanent home on my iPad. I like to read over them every now and then - keeps me grounded :-)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.