C++ POINTERSi wrote this to explain pointers to a friend... take a look...
pointers... yay!
basicly what a pointer is, is a variable that holds a memory adress a similar consept as a reference... and that is about it...
ok so now that you know that...
Declaring a pointer
lets say you got a var that is an int called iOMG and you want a pointer of it...
first create a pointer
- Code: Select all
int * pntOMG =0;
always initialize a poiner to 0 first or let the bad stuff happen
what bad stuff...?
lots of bad stuff... for instance...
- Code: Select all
float * pWhatEv;
for(i=0; i=100 ; pWhatEv = (pWhatEv * 2)/pWhatEv)
{}
what is pWhatEv pointing to? what memory are you changeing... what is the effect on the program/os/computer... only bad that's what.
"if you are lucky it will just crash" ~Jesse Liberty
ok so back to iOMG... we have the variable pntOMG defined to null/0 so we want to make it point to (get it... a pointer "points") iOMG.
so then you go
- Code: Select all
pntOMG = &iOMG;
that is it... all there is to it... plz take note of the & char... it says address of... or where the variable is in memory...of course you can do that in one fewer step...
- Code: Select all
int * pntOMG =&iOMG;
the problem here is that you better be sure iOMG is defined... or ELSE!
now using the pointer... the pointer is a variable that defaults to itself... you must tell it that you want the other thing that it points to SO...
- Code: Select all
pntOMG = 50; //sets the mem value to 50 ...aka BAD!
*pntOMG = 50;// sets &iOMG (or value pointed to) to 50... GOOD!
notice the char * which basicly means "the value stored at"...
note the pointer in NOT the same as the variable the BOTH exisit in memory... don't get confused!
ok so why would you want to do this EVER i mean you got the variable so why do it at all... well consider this...
- Code: Select all
void Sq(int mynum)
{
mynum= mynum*mynum;
return;
}
int main()
{
int mynum;
std::cin>>mynum;
Sq(mynum);
std::cout<<mynum;
return 0;
}
one could assume that mynum after going through that Sq function would be squared... right? WRONG! when you pass a variable into a function only a COPY of the variable is passed so while in the function 2 mynums exist each with a different scope, and because of that when the function passes the value couted is the same as inputed. so how do we fix this... well many ways... but lets use POINTERS!
- Code: Select all
void Sq(int * mynum)
{
*mynum= mynum*mynum;
return;
}
int main()
{
int mynum;
int * pnum = &mynum;
std::cin>>mynum;
Sq(pnum);
std::cout<<mynum;
std::cout<<*pnum;
std::cout<<pnum;
return 0;
}
i am pretty sure that is right... now when a copy of pnum is created it is a copy of the mem adress so nothing is different because both the original and the copy point to the same place...
this exaple could be done in much better ways without pointers but it shows why you would want pointers...
they also allow you to read and write values of private members of classes...(kind of a loop hole)
and the last reason to use them is to use the heap... which is basicly your memory... that is a whole other chapter in this book though and i will tell you about it if you get this stuff and you want to know...