Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ++(*p) is not giving l-value required error?

#include <stdio.h>
int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    int *p = arr;
    ++*p;
    p += 2;
    printf("%d", *p);
    return 0;
}

Why is this code not giving any compile time error,My doubt is ++*p is evaluated as ++(*p) and *p will be constant value 1 ,when we do ++(1) which is not a l-value,why is compiler not giving an error?

like image 277
Geeta Avatar asked Nov 29 '22 22:11

Geeta


1 Answers

*p will be constant value [...]

No, it is the same as arr[0].

So

++(*p);

is the same as

++(p[0]);

is the same as

++(arr[0]);

which is a perfectly valid statement.

like image 72
alk Avatar answered Dec 09 '22 10:12

alk