Thread: I'm trying to visualize pointers.

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    1

    I'm trying to visualize pointers.

    I've been reading alot about pointers, and I'm only half understanding them. I understand what pointers do. I understand how they are useful. I understand that declaring them and pointees. What I really need is to be able to visualize them so I can remember them and then use them more effectively in practice.

    So, if you will bare with me on my possibly odd analogy and let me know if I have this right or not...

    We have a fictional city named "Memory City". In Memory City there are lots of houses, each with their own addresses. On resident of Memory city is "Sue". Sue lives at "123 Some Street".

    So...

    If I wanted to tell the mailman where sue lived I would use "&SuesHouse" correct???

    If I wanted to get Sue out of her house I would use "*SuesHouse" (*SuesHouse == "Sue")???

    Bah, and I don't even know a good analogy for "SueHouse" to be used.

    ------

    Can anyone help me? Prefurably using a simular analogy?

    Thanks!

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by JesterJAG

    If I wanted to tell the mailman where sue lived I would use "&SuesHouse" correct???

    If I wanted to get Sue out of her house I would use "*SuesHouse" (*SuesHouse == "Sue")???

    Bah, and I don't even know a good analogy for "SueHouse" to be used.
    you're almost right. Assuming SuesHouse is the address of Sue, we have
    Code:
    Person Sue;
    Person *SuesHouse = &Sue;
    
    cout << "Sue lives at " << SuesHouse; // outputs "Sue lives at 0x1230BDEE" 
    
    cout << "the person living at " << SuesHouse << " is " << *SuesHouse; // assuming Person object have a << operator
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    You need the answer to your homework.

    The answer is at John's house. For some reason, you can't directly get the answer from John. (Maybe John doesn't even have a name.... but there's a house out there somewhere with the answer in it.)

    Sue's house is a pointer. She doesn't have the answer, but she has John's address, and she can get the answer from John for you.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by JesterJAG
    I've been reading alot about pointers, and I'm only half understanding them. I understand what pointers do. I understand how they are useful. I understand that declaring them and pointees. What I really need is to be able to visualize them so I can remember them and then use them more effectively in practice.

    So, if you will bare with me on my possibly odd analogy and let me know if I have this right or not...

    We have a fictional city named "Memory City". In Memory City there are lots of houses, each with their own addresses. On resident of Memory city is "Sue". Sue lives at "123 Some Street".

    So...

    If I wanted to tell the mailman where sue lived I would use "&SuesHouse" correct???
    Close. You would tell them SuesHouse, or &Sue.
    SuesHouse == &Sue ("The address of Sue") == "123 Some St."

    If I wanted to get Sue out of her house I would use "*SuesHouse" (*SuesHouse == "Sue")???
    Yes. Sue == *SuesHouse ("the thing at address SuesHouse")




    Another analogy is to imagine you have a filing system where each piece of paper is put in a uniquely numbered file.

    &Paper would give the number of the file that this paper is in.
    *File would give the contents of the specified file.

    A pointer then is just like writing down the number of one of the files on a post-it; a pointer is a variable that tells you where something else can be located. Rather than streets or files it's an address into the computer's memory.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM