# Pointers

• 11-23-2008
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.
• 11-23-2008
laserlight
We have a tutorial on pointers; have you read it?
• 11-23-2008
Nope, that might help eh?
• 11-23-2008
C_ntua
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:

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.
• 11-23-2008
Quote:

Originally Posted by C_ntua
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:

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?
• 11-23-2008
laserlight
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; };```
• 11-23-2008
C_ntua
Quote:

Originally Posted by chadmandoo
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.