As per my understanding, in the line marked as 'line 2' of the below code, the expression (*ptr)++
should generate "lvalue required" error because *ptr
evaluates to a constant value of i=1
, which is not lvalue?
So why is the program working successfully? Or am I somewhere wrong in my concepts? If yes, please enlighten me on this.
int main(void)
{
int i;
int *ptr = (int *) malloc(5 * sizeof(int));
for (i=0; i<5; i++)
*(ptr + i) = i;
printf("%d ", *ptr++); //line 1
printf("%d ", (*ptr)++); //line 2
printf("%d ", *ptr); //line 3
printf("%d ", *++ptr); //line 4
printf("%d ", ++*ptr); //line 5
}