Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Pointers(CrashCourse)


  • Please log in to reply
30 replies to this topic

#1 LogicKills

LogicKills

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 134 posts

Posted 26 August 2008 - 05:56 PM

Pointers In C++ (Crash Course)


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..

#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!!

#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~
#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

_________

So let's get some things straight p_myInt is a pointer the value it holds is an address.
However *p_myInt is an int, the value it holds is what ever the current value is at the address.


<DANGERS>
When you create a pointer, your computer allocates memory for the address, but not for the data that will be at the address..

Example:

long* myPointer;
*myPointer = 13373773773;

So yes myPointer is a pointer, however we have neglected to say where it points address wise.
So this can cause major problems.
ALWAYS INITIALIZE YOUR POINTERS~!


__________

Alright so now you know a good amount about what pointers are an how they work, my next tutorial will be what you can do with pointer's and when they come in handy!

I hope this threw some light on the shady subject of pointers!

Next to come:
Using pointers, using new and delete, and some other fun stuff!

/LogicKills/
  • 7
http://logickills.org
Science - Math - Hacking - Tech

#2 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 27 August 2008 - 07:07 AM

Excellent tutorial! +rep
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 27 August 2008 - 07:09 AM

When he says there is no memory allocated for the data, you can use malloc, calloc, or new to do it.
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 27 August 2008 - 08:02 AM

Pointers, their "absence" is one of my biggest issues with Java.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 LogicKills

LogicKills

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 134 posts

Posted 27 August 2008 - 12:31 PM

When he says there is no memory allocated for the data, you can use malloc, calloc, or new to do it.


Yes, I am quite aware.
However I didn't want to include 'new' in this tutorial.
  • 0
http://logickills.org
Science - Math - Hacking - Tech

#6 LogicKills

LogicKills

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 134 posts

Posted 27 August 2008 - 12:38 PM

Thanks Xav!
  • 0
http://logickills.org
Science - Math - Hacking - Tech

#7 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 27 August 2008 - 12:52 PM

Excellent tutorial! +rep
  • 0

#8 MeTh0Dz

MeTh0Dz

    CC Leader

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1726 posts

Posted 27 August 2008 - 01:01 PM

Lol, I'm sure you are aware. But some readers may be wondering how they can allocated this memory.
  • 0

#9 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 28 August 2008 - 05:56 AM

Excellent tutorial! +rep

It's funny how my +reps are worth over double your +reps... :)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#10 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 28 August 2008 - 09:15 AM

I can +rep you unlimited times in a row though. I can also -rep people for trying to get me to increase my +rep. :P
  • 0

#11 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 28 August 2008 - 09:39 AM

LOL :D

You can +rep me unlimited times... but do you want to?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#12 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 28 August 2008 - 11:32 AM

I might, if impressed enough but it isn't very probable.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download