Quote:
Originally Posted by amateurrobotguy
I don't get some of your example:
Code:
void idouble(int * input){ Input the address and convert it to the data "input"
*input *= 2 ; I don't get the use of *input
}
void main(){
int x ;
x = 5 ;
idouble(&x) ; Call the above function giving it an address as a parameter
print(x); I don't get how the idouble() returns x
}
|
input is a pointer. it holds an address. in this particular case, it holds the address of x. putting a * in front of a pointer means that you want to look at the value held at the address of the pointer. if input = &x, *input = x. so modifying *input will actually be modifying the value held in x. (input is a pointer, so it has no real value attached to it.) By doing this, you can modify variables in your main function without returning ANYTHING from a called function. idouble returns nothing, but because you are telling it where x is in memory and not just the value of x, you can actually change x from another function. Make sense?