there are two ways that you pass things to functions: pass by value, and by reference. You need to understand what these mean, right? Here is an example:
Code:
struct tes{
int x, y, z;
};
void foocopy(tes var){
// do something here, bla bla
}
void foorefer(tes& var){
// do something here, bla bla
}
When you call foocopy(tes var) a brand new copy of the struct tes is made when the function is called, so if you did this:
tes v;
v.x=v.y=v.z=6;
foocopy(v);
no matter what you do to the variable inside foo, v will never change!, Why? Because you passed it by value, which means a NEW COPY is made when the function is called and that is what the function works with.... a copy!
BUT, if you do this :
tes v;
v.x=v.y=v.z=6;
foorefer(v);
A new copy is NOT made and anything you do inside the function foorefer ACTUALLY modifies v.
This is very usefull because sometimes you do not want to MODIFY the variable passed to a function, and sometimes you do. There is also SPEED considerations here. Each time a variable is COPIED, there is extra work. Passing something by reference is the fastest, because no copies are made or anything.
You need to know what POINTERS actually are first!
ANY pointer, of any type, i.e. int*, float*, tes* ANY pointer!!!! is really a memory address, or more Precisely, A NUMBER.
when you typed this in your code.............
function2(&variable);
the &variable will Get the Memory Address of variable and pass that to function2. pointers are memory addresses, mmkay?
so, when function2 runs, it receives the memory address ( also known as a pointer), called var. SINCE you did not pass by reference, A COPY IS MADE of the pointer, or memory address that was passed.
Well, what the hell does a * do then? A * in front of a pointer type GETS the value stored at the memory address that it holds. In this case, you are telling the compiler to place the value of 10 at the memory address held by var. var is holding the memory address of variable, which is why it changes!