操作符++作为前缀和后缀的优先级不一样,当它与指针相结合时,理解起来可能就更复杂一些,以下代码列举了其中的各种情形。
int
main()

...
{
char arr1[] = "hello world!", arr2[] = "HELLO WORLD!";
char *p = arr1, *q = arr2;

// the priority of ++ is higher than *
// so firstly p++, return a temporary _p,
// then *p, *p is valid, it can be lvalue;
*p++ = *q++;

// the same as above
*(p++) = *(q++);

// firstly p++, return a temporary _p,
// then ++_p, _p can't be lvalue, so compiler complains
++p++ = ++q++;

// the similar as above, _p can't be lvalue
p++ = q++;

// this is ok, no temporary occurs
++p = ++p;

// this is ok, the same as ++(*p);
++*p = ++*q;

// the priority of ++(postfix) is higher then ++(prefix),
// so firstly p++, return a temporary _p,
// then ++_p, but compiler expects a lvalue, so it complains
++p++;

// this is same as (p++)++;
// so firstly is p++, return a temporary _p,
// then _p++, but compiler expects a lvalue, so it complains
p++++;

// the same as above, the priority of * is lower then ++(postfix);
*p++++;

// this is ok. firstly p++, return a temporary _p,
// then *_p, it is valid as vp,
// the last is ++vp
++*p++;

// this is ok, the same as *(++(++p))
*++++p = *++++q;

int a = 0, b = 0, c = 0;

// the priority is: ++(postfix), then +, then ++(prefix)
// so this is same as c = (((a++)++)+b);
// in a++++, compiler expects a lvalue, so it complains
c = (a+++++b);

return getchar();
}
























































