1

In the Following example, I expect that foo((&i)++) will evaluate to foo(4 + address of (i)) assuming that the int size is 4 Byte however it gives a compilation error at this line

anyone has an explanation ?

void foo(int*);
int main()
{
    int i = 10;
    foo((&i)++);
}
void foo(int *p)
{
    printf("%d\n", *p);
}
  • 1
    Also `foo(1234++)` or `foo((i*2)++)` don't compile, for similar reasons. – Basile Starynkevitch Jan 29 '14 at 00:58
  • Even if you could use `++` on an rvalue, postfix `++` evaluates to the original value, not the incremented value. – user2357112 Jan 29 '14 at 00:59
  • In your question, the thing is `++` operator can't applied on an expression and you are applying `++` on `&i` that is an expression. Assume `i++` is like `i = i + 1;` similarly `(&i)++;` as `&i = &i + 1;` Can you assign to an expression? That what compilation error: Read this Q&A also [Why `++i++` gives "L-value required error" in C?](http://stackoverflow.com/a/17850934/1673391) – Grijesh Chauhan Feb 16 '14 at 05:25

3 Answers3

6

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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Floris
  • 45,857
  • 6
  • 70
  • 122
3

The operand of ++ must be an lvalue -- a variable or other location in memory that can be modified -- since it adds 1 to it and replaces it with the result. &i is not an lvalue, it's just an expression that yields the address of i.

Also,

foo(4 + address of (i))

is wrong since you're using postfix ++ rather than prefix ++. The value of EXPR++ is EXPR (with the side effect of changing the variable that expr refers to).

To get the value you want, just use

foo(&i + 1)

Note, however, that this is likely to result in undefined behavior ... depending on just what foo does with its argument.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
0

You're incrementing and trying to store back the address of i. How and where does the result get stored back? It can't, because &i doesn't exist in memory.

I think you want to do this instead:

 foo((&i)+1);
tangrs
  • 9,709
  • 1
  • 38
  • 53