Search code examples
cpointersfunction-pointers

can you explain why compile time error in the below code


why im getting errors in the code given below....

#include <stdio.h>
    void foo(int*);
    int main()
    {
        int i = 10;
        foo((&i)++);(in this line error shows like this) //error: lvalue required as increment operand
    }
    void foo(int *p)
    {
        printf("%d\n", *p);
    }

Solution

  • From Member access operators:

    The address-of operator produces the non-lvalue address of its operand, suitable for initializing a pointer to the type of the operand.

    And from Increment/decrement operators:

    The operand expr of both prefix and postfix increment or decrement must be a modifiable lvalue of integer type (including _Bool and enums), real floating type, or a pointer type.

    Simply put the & operator does not produce an appropriate object for the ++ operator.