Since the previous answers contradict each other I found out.
// gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)
#include "stdio.h"
int main()
{
int array[] = {0,10,20,30,40,50,60};
int* p = &array[1];
int r;
int i = 1;
r = *p;
printf("%d, r = *p: r=%d, p points to %d\n", i++, r, *p);
r = *p++;
printf("%d, r = *p++: r=%d, p points to %d\n", i++, r, *p);
r = ++*p;
printf("%d, r = ++*p: r=%d, p points to %d\n", i++, r, *p);
r = *++p;
printf("%d, r = *++p: r=%d, p points to %d\n", i++, r, *p);
r = ++*p++;
printf("%d, r = ++*p++: r=%d, p points to %d\n", i++, r, *p);
// ++p++;
printf("%d, ++p++: error: lvalue required as increment operand\n", i++);
// r = *++p++;
printf("%d, *++p++: error: lvalue required as increment operand\n", i++);
// (++p)++;
printf("%d, (++p)++: C error: lvalue required as increment operand\n", i++);
printf("%d, ++p++: C++ p points to %d\n", i++, *p);
// ++(p++);
printf("%d, ++(p++): error: lvalue required as increment operand\n", i++);
return 0;
}
Output:
1, r = *p: r=10, p points to 10
2, r = *p++: r=10, p points to 20
3, r = ++*p: r=21, p points to 21
4, r = *++p: r=30, p points to 30
5, r = ++*p++: r=31, p points to 40
6, ++p++: error: lvalue required as increment operand
7, *++p++: error: lvalue required as increment operand
8, (++p)++: C error: lvalue required as increment operand
9, ++p++: C++ p points to 60
10, ++(p++): error: lvalue required as increment operand
1 dereference as expected
2 dereference p and then increment it
3 increment the location pointed to by p, p unchanged
4 increment p and then dereference it
5 increment the thing pointed to by p (++*p)
and then increment p (p++)
6,7, 10 can not increment an intermediate value because it does not have a storage location.
8, 9 are "interesting". The C compiler complains about the lack of an lvalue but C++ does not. Instead, as might be expected, it increments p twice. However p += 2
is preferable as it is portable and easier to understand.