Quote:
Originally Posted by amateurrobotguy
Why bother with the int *ptr declaration when it has to be used again in the b= part? This is the example from Wiki.
|
Code:
int *ptr; //ptr is a pointer to the address an 'int' somewhere in memory
int a, b; //compiler chooses where these go in memory, each have an address
a = 10; //set value of a to 10
//pass a by reference to ptr
ptr = &a; //ptr now holds the address of where a is in memory
b = *ptr; //b now holds the value (use *) that ptr points to
In this case, b would be equal to a at the end.
The first line declares a pointer, which is different from a regular variable.
You can do other cool things with this, too.
Code:
void idouble(int * input){
*input *= 2 ;
}
void main(){
int x ;
x = 5 ;
idouble(&x) ;
print(x);
}
This will print 10.