"The word 'radical' derives from the Latin word for root. Therefore, if you want to get to the root of anything you must be radical. It is no accident that the word has now been totally demonized..." -- Gore Vidal
Before We Get Started:
First lets talk briefly about computer memory and how it's addressing works.
Just like your house, the computer has an address for every memory block.
However instead of 124 LeeT Haxz0r Street, it is in a hexadecimal format.
So it looks something like ~ 0x00000000. Lets look at an example..
Code:
#include <iostream>
int main()
{
int myInt;
std::cout << "The address of \'myInt\' is: " << (&myInt) << std::endl;
return 0;
}
Ok, lets analyze this code.
First we declare an variable of type int called 'myInt'.
Then we use 'std::cout <<' to print the address of the variable to the screen.
This is possible because of the little '&' symbol. This is called the address operator.
Okay let's compile this thing..
<Linux>
$ g++ example.cpp -o example
$
$./example
$The address of 'myInt' is: 0xbfcbfab0
_________
<Windows>
Dev C++..
[F9 Key]
note: If you are compiling and running on widows, and your window vanishes before you can see the output.. add one of these:
'cin.get();'
__________
Alright if you compiled and ran it successfully congrats, please note that the memory address will vary. Alright since you understand a little bit how addressing works lets move on to actual pointers.
__________
Okay, so now with that all out of the way what do 'pointers' actually do.
Well their name is actually the thing they do, they point.
What they do is point to another address, this could be the address of a variable or something a little more complex like a function (we won't cover that yet).
In the example above we found out the address of our int by using that handy little operator called the.... yep, you guessed it: The Address Operator!
Lets open up that program and actually use a pointer and not just print the address to the screen..
Get your IDE's ready!!
Code:
#include <iostream>
int main()
{
int myInt;
int* p_myInt = &myInt; // in this case p stands for pointer :p
std::cout << "The address of \'myInt\' is: " << (&myInt) << std::endl;
std::cout << "The address of my pointer \'p_myINt\' is: "<< p_myInt << std::endl;
return 0;
}
Alright compile and run that!
Again the address will vary!
So for my output I got:
The address of 'myInt' is: 0xbfe52570
The address of my pointer 'p_myINt' is: 0xbfe52570
Hah!! The addresses match, so we successfully used a pointer.
Alright, pretty cool eh?
Wouldn't it be cooler if you could actually see the value that is at that address?
Well it turns out you can :]
To do that we are going to use the '*' dereferencing operator!
We are going to edit that program again a minuscule amount, let's assign a value to myInt~
Code:
#include <iostream>
int main()
{
int myInt = 6;
int* p_myInt = &myInt; // in this case p stands for pointer :p
std::cout << "The address of \'myInt\' is: " << (&myInt) << std::endl;
std::cout << "The address of my pointer \'p_myInt\' is: "<< (p_myInt) << std::endl;
std::cout << "The VALUE of myInt is : " << (myInt) << std::endl;
std::cout << "The VALUE of *p_myInt is : " << (*p_myInt) << std::endl;
return 0;
}
Compile and run it
Here is my output~
The address of 'myInt' is: 0xbfb2810c
The address of my pointer 'p_myInt' is: 0xbfb2810c
The VALUE of myInt is : 6
The VALUE of *p_myInt is : 6
_________
Cast your vote on this article 10 - Highest, 1 - Lowest
Comments: Published: 8 comments.
HackThisSite is the collective work of the HackThisSite staff, licensed under a CC BY-NC license.
We ask that you inform us upon sharing or distributing.