4

I find that I learn best through real-world comparison metaphors. For example, in java, one way that helped me understand the concept of Accessors and Mutators really well was thinking of it through the concept of a middle man (the private variables don't like to deal with their clients directly, they prefer to have a middle man(the accessors and mutators) handle them)

The concept of pointers in C is still kinda tricky for me. What would be a good metaphor to use to describe how they work?

user1768884
  • 1,079
  • 1
  • 20
  • 34

5 Answers5

12

I always liked the analogy that a pointer is like your finger. If I take you to a parking lot and ask you to find me a red car, you extend one finger (of your choice ;)) and point to a red car.

Your finger is not the answer. Your finger tells me nothing, but if I look where you're finger is pointing to, I can find what I was looking for.

Now I can ask you to find a blue car and you can redirect your finger (reassign) it to a new car. Now your pointer (the same one as before) is showing me new data of the same type. The pointer hasn't change, just what it is pointing to.

This works with more than one pointer as well. I can ask where is the pointer pointing to the blue car and you can use your other hand and point with a finger to the first finger. Now if I want to know where the blue car is I can follow the first finger's direction to the second finger, to the car (the data).

Mike
  • 47,263
  • 29
  • 113
  • 177
1

You can think of then just like real world addresses, they just tell you where to find stuff in memory, incrementing a pointer (i am refering here to pointer arithmetic) is just like moving to the house next door, have a look at this answer to another question.

Community
  • 1
  • 1
A4L
  • 17,353
  • 6
  • 49
  • 70
0

You can think of them like phone numbers, which tell you how to reach someone.

Calling the phone number is like dereferencing a pointer.

A phone could be disconnected. Maybe it's been a long time, and now someone else has the phone number. That's like a dangling pointer.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • the metaphor works for a single pointer, but what about pointer to pointer to int, for instance ? – stdcall Apr 04 '13 at 18:14
  • 2
    A phone number for a person who knows the phone number of the next person? – Gavin H Apr 04 '13 at 18:15
  • Pointer to a pointer is a phone number to a company reception desk. That persona at the reception desk can then direct/forward the call to the final person you are trying to reach (connecting you to their extension). – StarPilot Apr 04 '13 at 18:17
  • Yeah, I like Gavin's suggestion. Like all similes/metaphors/analogies, there will be some things that don't fit. Feel free to point out problems or additional aspects that fit. I've made it community wiki, so go ahead and edit if you like. – Fred Larson Apr 04 '13 at 18:20
  • @FredLarson How your answer become `community wiki` just in one revision. – Grijesh Chauhan Apr 04 '13 at 18:21
  • @GrijeshChauhan: I edited and checked the "Community wiki" box. – Fred Larson Apr 04 '13 at 18:22
0

Just suppose a scenario of a spreadsheet, You have data scattered here and there, but when you can call a specific data using row and column numbers, you get to the right data.

The rows and columns are pointers.

Now you can store these row and column numbers into another spreadsheet, for your reference. And this spreadsheet also now has row and column numbers to determine the data, which is a reference to the actual data to the original spreadsheet.

cipher
  • 2,414
  • 4
  • 30
  • 54
-3

No need for metaphors. A pointer is a variable that holds a memory address. Period.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • He didn't ask do he need it or not, he asked for metaphore if anyone can come up with some, as far as I understood, correct me if I'm wrong – Sava Vranešević Apr 04 '13 at 18:20
  • @WingsOfIcarus I understood that. But often, trying to create a metaphor (especially for trivial things) can cause you to actually understand **less** of the concept. – Bart Friederichs Apr 04 '13 at 18:21
  • a lot of people - especially beginners - have trouble understanding them, so it is not as trivial as you think. maybe they are when you **fiiiinally** understand them, at this point may be they become trivial. – A4L Apr 04 '13 at 18:25
  • @Bart Friederichs True that, but if you wanna explain it to someone who is new to programming it's not bad idea to use metaphor for it, since most of people tend to have problems with understanding pointers at first. – Sava Vranešević Apr 04 '13 at 18:25
  • @WingsOfIcarus I know. And metaphors can really help understanding the concept. But a clean, correct definition to fall back to is also always helpful. – Bart Friederichs Apr 04 '13 at 18:34
  • You need to have a concept first. And using good analogy is the best way to get a good concept. Did your teacher teach you counting straight to numbers or using things like apples, or fingers? – Zarul Zakuan Jun 13 '20 at 00:27