Jump to content

Pointers(CrashCourse)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
30 replies to this topic

#1
LogicKills

LogicKills

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
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/
http://logickills.org
Science - Math - Hacking - Tech

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Excellent tutorial! +rep
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

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

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Pointers, their "absence" is one of my biggest issues with Java.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
LogicKills

LogicKills

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts

MeTh0Dz|Reb0rn said:

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.
http://logickills.org
Science - Math - Hacking - Tech

#6
LogicKills

LogicKills

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
Thanks Xav!
http://logickills.org
Science - Math - Hacking - Tech

#7
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Excellent tutorial! +rep

#8
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Lol, I'm sure you are aware. But some readers may be wondering how they can allocated this memory.

#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

Jordan said:

Excellent tutorial! +rep
It's funny how my +reps are worth over double your +reps... :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#10
Guest_Jordan_*

Guest_Jordan_*
  • Guests
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

#11
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
LOL :D

You can +rep me unlimited times... but do you want to?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#12
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I might, if impressed enough but it isn't very probable.