int main()
{
int i;
f(&i);
cout << i;
return 0;
void f(int *j)
{
*j = 100;
}
}
I don't understand why f(&i) will work
&i is the address of i
should void f(int *j) change to void f(int &j)?
I understand I must be wrong, but what the meaning difference fot those two expressions??
You could have posted this question in the other thread.
f(&i);
This means "pass the address of i as the first parameter of f() and call the function". void f(int *j)
This, on the other hand, means "define a function named 'f' that takes a pointer to an int as its first parameter and returns nothing". Notice how the declaration of the parameter is identical to a declaration of a pointer anywhere else. If you changed the declaration to void f(int &j), that means an entirely different thing.
Whenever you're confused about how something should be declared, try it on the compiler and see what happens. If you're right, the compiler will say nothing. Otherwise, it will complain about type mismatches.