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.
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 ...
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.
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
Nope, that might help eh?
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:
then you might have this:Code:int* myPtr;
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:
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:Code:myPtr = &myInt;
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:
you can do this, with the same result:Code:int b; b = myInt;
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.Code:int b; b = *myPtr;
That is all. Don't know if it makes sense.
Last edited by C_ntua; 11-23-2008 at 10:02 AM.
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?
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).Originally Posted by chadmandoo
Yes, but because member variables are usually private, this should not happen very often.Originally Posted by chadmandoo
For example:Originally Posted by chadmandoo
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