Variable, like y and z in your program, have ranges. They exist only in a limited logical space. For instance, y and z only "exists" in main ; that's why they're called "local variable". Would you write :
int x,y;
void main
{
// Stuff
}
x and y would be global variables, that any function in this file could access. Since global variable make things harder to debug, when you need to access variable declared in a function, you can use pointer (it's only one of the uses of pointers, useful in your example).
When you give parameters to a function, by default, those parameters are copies, not the original. So, function(int p, int q) creates two new variables : you can do whatever you want to q and p, the x and z would not change.
When you use pointers, you create a special variable, containing the adress of the variable. You can get the adress of any variable with the "&" sign. This is why, to call function(int* p, int* q) you need to write function(&y, &z).
To access the content of a pointer, you need to use the * sign in front of the pointer. Beware ! If you don't do this, you're not modifying the variable the pointer is pointing to, but the adress of the pointer (in your case, that would not be useful ; but in array manipulation, for instance, it can be very useful).
So, your function gets the adresses of two variables, and then do some computing.
This function does two things :
- It computes something and give the result ;
- During the computing, it gives to q the value of p, so z's value becomes y's value in the main.
Do some printf in your main, checking the value of your variables, before and after the function. Do it with a function using pointers and another one that does not use them, and it'll be much clearer to you.
And add a "int" in front of your function, since it returns something.
(*p + *q) - (*q = *p)seems to depend on which operand is first executed, which is undefined ?( * p = ( * p + * q ) - ( * q = * p ) ). Everything else is personal preferences.