Recent Articles
Check Out
- Tech Today
- and our
- Computer Tutorials
- or join the discussion @ our
- Computer Forum
Search Articles
C++ Pointer Basics
Dec 16 2011 11:37 PM | mr.d in Programming Tutorials
In this short tutorial, we will take a look at some of the basics of C++ Pointers.
What is a pointer? A pointer stores the memory address of a variable, it does not store the actual value. What does this really mean?
Let's declare for example a simple integer variable myVar and give it a value:
When you cout myVar, the output is 10. Try it out:
Now let's create a pointer to myVar, and take a look at how it works as well as the syntax involved.
In the above snippet we defined a variable myVar and assigned it's address to the variable point using the reference operator " & ". Try outputting the value of each, you will get different results! myVar will give you an output of "10", while the output of point will give you the address of the variable on your system.
So how do we get the value stored at that address? Simple.
Using the dereference operator, we can view the actual value stored at the memory location pointed to by point. So
will give you the same output.
We can also modify the value stored in myVar through the pointer.
myVar is no longer equal to 10, but instead 15. You can check with a simple cout of myVar.
Pointers become extremely important when working with functions, since instead of making a copy of a variable or object passed to the function, we can pass them by reference and perform modifications right from within the function... We also have to exercise caution doing so, because sometimes we do not want the original variable modified, but more on that at a later date!
This was just a simple look at how pointers work in C++, and is meant to be a simple introduction! Pointers are a great tool, and one of the most commonly used, and to get a real good understanding I recommend buying a C++ book!
Thanks for looking!
What is a pointer? A pointer stores the memory address of a variable, it does not store the actual value. What does this really mean?
Let's declare for example a simple integer variable myVar and give it a value:
int myVar=10;
When you cout myVar, the output is 10. Try it out:
cout << myVar;
Now let's create a pointer to myVar, and take a look at how it works as well as the syntax involved.
int myVar = 10;
int point = &myVar;
In the above snippet we defined a variable myVar and assigned it's address to the variable point using the reference operator " & ". Try outputting the value of each, you will get different results! myVar will give you an output of "10", while the output of point will give you the address of the variable on your system.
So how do we get the value stored at that address? Simple.
cout << *point ;
Using the dereference operator, we can view the actual value stored at the memory location pointed to by point. So
cout << myVar;
cout << *point;
will give you the same output.
We can also modify the value stored in myVar through the pointer.
int myVar=10;
int point=&myVar;
*point=15;
myVar is no longer equal to 10, but instead 15. You can check with a simple cout of myVar.
Pointers become extremely important when working with functions, since instead of making a copy of a variable or object passed to the function, we can pass them by reference and perform modifications right from within the function... We also have to exercise caution doing so, because sometimes we do not want the original variable modified, but more on that at a later date!
This was just a simple look at how pointers work in C++, and is meant to be a simple introduction! Pointers are a great tool, and one of the most commonly used, and to get a real good understanding I recommend buying a C++ book!
Thanks for looking!











0 Comments