What is a pointer?

A descriptive and ELI5 answer found on Reddit:

You have a house.

When you’re outside, and you want to go home, you don’t make another house right where you are, because it’s too big for you to carry around or take apart.

So you carry a piece of paper or store on your phone the address of your house. Now you always know exactly how to find your house so that you can go home.

The piece of paper or your phone is a variable.

The variable contains an address (a reference) to your house.

You can make a copy of this piece of paper and hand it out to your friends when you invite them over, instead of building each friend a copy of your house.

You can have an address book filled with pages, where each page is an address (i.e., an array of pointers). Each page you turn (each index you increment) goes to the directly next address-containing variable.

Now if you cut the address book in half along its height, and then attach the lower half behind the upper half, then you have a book with smaller pages but more pages.

You can store phone numbers in this, and even if it’s the same total size, you have double the number of pages and double the number of phone numbers (if you store one number on each page).

Now, since the pointers to home-addresses are different from pointers to phone-numbers, turning the page in an address book (increment pointer by 1) moves forward by one address.

But turning the page in the phone book (incrementing pointer by 1) leads you to the next phone number, even if you technically turned only “half a page”.

That’s how pointer arithmetic works.

Source: https://reddit.com/r/...

  • 25
    This is somehow more confusing than pointers.
  • 6
    I don't think newbies struggle with the concept of pointers.
    They struggle with the implementation and special cases
  • 3
    @Konsole they do
    This is the similar thing to what I told few students to make them understand it
  • 9
    You had me up until splitting the address book in half. That doesn't sound like anything I have ever done.
  • 4
    When I was learning it was for me easier to understand when I saw the code and printed the values of pointer and array to understand what's going on. It's a good story, but can be replaced with a minute or two of hands-on coding experience.
  • 0
  • 5
    Me: *goes home following address*
    C++: *deletes house*
    Me: Ah, shit here we go again
  • 6
    What is a pointer?

    It's a reference to memory. A an actual integer number "pointing" to which "sector" of memory your data starts at.

    You: Actually, it's about a three page analogy.
Add Comment