The error message is "lvalue required for increment operator". The problem is that ++
needs to operate on a variable - you increment, AND STORE THE RESULT.
You cannot store the result of the increment operation in (&i)
.
To get foo
to operate on an integer that is stored at the address you appear to want, you can do one of the following (I'm sure you can think of others):
foo(&i);
int *p = &i; foo(p++);
The second option will correctly call foo
with a pointer to i
, but will increment that pointer for the next time (which seems to be what you were trying to do with your code - except you had nowhere to put that value. By declaring a separate pointer p
, I created that storage space. But realize that p
is now pointing "nowhere" - if you access it again, you will get undefined behavior).
If you wanted to point to the next location after the address of i you would have to do
foo(++p);
but that would be undefined behavior (since there is no way of knowing what is stored in the next location after i; most likely it will be p
but that is not guaranteed.)
Pointers. Powerful, dangerous, and slightly mysterious.