Pointers

This is a discussion on Pointers within the C++ Programming forums, part of the General Programming Boards category; Can anyone put this into terms that I would understand? I am a noob trying to learn C++. Also anyone ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    5

    Pointers

    Can anyone put this into terms that I would understand? I am a noob trying to learn C++. Also anyone have good tutorials for C++? I am trying to learn C++ and Microsoft Visual C++ at the same time.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    We have a tutorial on pointers; have you read it?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Nope, that might help eh?

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I ll give you a description providing you know the basics of the memory. If you don't like my way of describing it just skip the post :P. There are better ways to describe it. Just google if you want.

    Generally, a pointer is a variable that points to other variables. It does this by storing their memory address. So if you have variable A and you have pointer B then you can set B to point at A. Thus you will have

    B --> A

    You can now change the value of A through B. You can make B point to another variable as well. That is the usefulness of a pointer.

    More detailed. First of all, all variables are places in the memory. Imagine the memory as a set of blocks. Each block has an address and a value. Lets say we have this two memory blocks which both have a value 0. Like:

    Address Value
    1234 [0]
    1235 [0]

    Now, when you declare a variable, lets say int myInt then you "allocate" one memory space. In other words you give a name to a memory space. So you would have:

    1234 [0] myInt
    1235 [0]

    So, when you do myInt = 12 + 2, then the value 16 will be stored in the memory address 1234, which is the memory location that is named myInt. So you would have:

    1234 [16] myInt
    1235 [0]

    No, what is a pointer. A point is again a variable. It has normally values. The difference is that this values refer to memory address. Like if you do this:
    Code:
    int* myPtr;
    then you might have this:

    1234 [16] myInt
    ...
    1241 [...] myPtr

    The memory address the variable is stored is something the system chooses for you. Just use random number for my example. So if you do this now myPtr = 12 again you will have:

    1234 [16] myInt
    ...
    1241 [12] myPtr

    The point is NOT to do that though. The value has to be some address in the memory. Lets say you want myPtr to point to myInt. Then you would store the memory address of myInt to myPtr. You wouldn't do though myPtr = 1234 because you don't know the memory address, and you don't need to know! You do this:
    Code:
    myPtr = &myInt;
    The & symbols takes the address of the variable. What is the address of myInt?? It is 1234. So now your memory will look like this:

    1234 [16] myInt
    ...
    1241 [1234] myPtr

    You will want to think it like this though:

    1234 [16] myInt
    ...
    1241 [&myInt] myPtr

    Because you don't care about the actual number. Why would you do all this, you ask? Because now you can use the value of myInt using myPtr. To do that you use the * symbol. Lets say you want to put in b the value of myInt. You know that myPtr "points" to myInt. So instead of doing this:
    Code:
    int b;
    b = myInt;
    you can do this, with the same result:
    Code:
    int b;
    b = *myPtr;
    How is this achieved? The program reads the value of myPtr. Which is &myInt, or in other words 1234. You tell it to get the value from that address (since you have the * symbol). So it will take the value from the memory address 1234. Thus, the value 16.

    That is all. Don't know if it makes sense.
    Last edited by C_ntua; 11-23-2008 at 10:02 AM.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    5
    Quote Originally Posted by C_ntua View Post
    I ll give you a description providing you know the basics of the memory. If you don't like my way of describing it just skip the post :P. There are better ways to describe it. Just google if you want.

    Generally, a pointer is a variable that points to other variables. It does this by storing their memory address. So if you have variable A and you have pointer B then you can set B to point at A. Thus you will have

    B --> A

    You can now change the value of A through B. You can make B point to another variable as well. That is the usefulness of a pointer.

    More detailed. First of all, all variables are places in the memory. Imagine the memory as a set of blocks. Each block has an address and a value. Lets say we have this two memory blocks which both have a value 0. Like:

    Address Value
    1234 [0]
    1235 [0]

    Now, when you declare a variable, lets say int myInt then you "allocate" one memory space. In other words you give a name to a memory space. So you would have:

    1234 [0] myInt
    1235 [0]

    So, when you do myInt = 12 + 2, then the value 16 will be stored in the memory address 1234, which is the memory location that is named myInt. So you would have:

    1234 [16] myInt
    1235 [0]

    No, what is a pointer. A point is again a variable. It has normally values. The difference is that this values refer to memory address. Like if you do this:
    Code:
    int* myPtr;
    then you might have this:

    1234 [16] myInt
    ...
    1241 [...] myPtr

    The memory address the variable is stored is something the system chooses for you. Just use random number for my example. So if you do this now myPtr = 12 again you will have:

    1234 [16] myInt
    ...
    1241 [12] myPtr

    The point is NOT to do that though. The value has to be some address in the memory. Lets say you want myPtr to point to myInt. Then you would store the memory address of myInt to myPtr. You wouldn't do though myPtr = 1234 because you don't know the memory address, and you don't need to know! You do this:
    Code:
    myPtr = &myInt;
    The & symbols takes the address of the variable. What is the address of myInt?? It is 1234. So now your memory will look like this:

    1234 [16] myInt
    ...
    1241 [1234] myPtr

    You will want to think it like this though:

    1234 [16] myInt
    ...
    1241 [&myInt] myPtr

    Because you don't care about the actual number. Why would you do all this, you ask? Because now you can use the value of myInt using myPtr. To do that you use the * symbol. Lets say you want to put in b the value of myInt. You know that myPtr "points" to myInt. So instead of doing this:
    Code:
    int b;
    b = myInt;
    you can do this, with the same result:
    Code:
    int b;
    b = *myPtr;
    How is this achieved? The program reads the value of myPtr. Which is &myInt, or in other words 1234. You tell it to get the value from that address (since you have the * symbol). So it will take the value from the memory address 1234. Thus, the value 16.

    That is all. Don't know if it makes sense.
    This makes very good sense. But why change a value through a pointer instead of changing it through the variable itself? Can an pointer reference to a variable in a different class than it is in? Say a pointer is in Class1() and wants to reference a variable in Class2(), is this how you would reference this?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by chadmandoo
    But why change a value through a pointer instead of changing it through the variable itself?
    The pointer may be the only way to refer to the object pointed to. Take a dynamically allocated array, for example. You cannot access any element of the array except via a pointer (and in fact this applies to fixed size arrays as well, but you have to understand that arrays are converted to pointers to their first element).

    Quote Originally Posted by chadmandoo
    Can an pointer reference to a variable in a different class than it is in?
    Yes, but because member variables are usually private, this should not happen very often.

    Quote Originally Posted by chadmandoo
    Say a pointer is in Class1() and wants to reference a variable in Class2(), is this how you would reference this?
    For example:
    Code:
    class Class2
    {
    public:
        int x;
    };
    
    class Class1
    {
    public:
        void foo(Class2& obj)
        {
            p = &obj.x;
        }
    
        int* p;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by chadmandoo View Post
    This makes very good sense. But why change a value through a pointer instead of changing it through the variable itself?
    Well, there are a lot of uses. Think of a linked list for example. Each node has to "connect" to the next node. This will be done with a pointer. The node needs to know where the other node is located, so that information can only be stored in a pointer variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21