edited by
2,293 views
2 votes
2 votes
void foo(int*);
int main()
{
 int i = 10;
 foo((&i)++);
}
void foo(int *p)
{
printf("%d\n", *p);
}


It gives compile time error but how? which particular statement is cause of this error in program?

edited by

1 Answer

Best answer
3 votes
3 votes

The Increment operator requires lvalue is an expression referring to an object. 

We know that,

int a = 10; // a is lvalue expression and 10 is rvalue which is usually a constant.

 int *p = &a; // Note that  An lvalue can also be an rvalue or can be used in rvalue, but an rvalue can never be an lvalue.


From the above statement &lvalue is a rvalue.

&i ++ = (rvalue)++, but increment operator requires lvalue as its operand. Hence compiler error.


Error can be resolved as ,

    int i = 10;
    int *p = &i;
    foo(p++); 

selected by

Related questions

0 votes
0 votes
1 answer
1
Debargha Mitra Roy asked 3 days ago
51 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...