So I'm quite confused as to how the post-increment is not evaluated first. (According to precedence post-increment comes first.) Can someone please explain this?
Consider the code given below:
int main()
{
char arr[] = "iitbforgoodlife";
char *ptr = arr;
while(*ptr != '\0')
++*ptr++;
printf("%s %s", arr, ptr);
getchar();
return 0;
}
The output is: jjucgpshppemjgf
The post-increment, being a primary expression operator, has highest precedence. The result of the operator is the original value (the increment may happen any time before the end of the sequence point).
Next is the unary operator
*which dereferences the pointer to return the underlying object, which is a character from the arrayarr.Finally, the pre-increment operator, also a unary operator, which has the same precedence as
*. However, it is applied later because operators of the same precedence are associated in some precedence specific order. For unary operators, they are associated right-to-left. Therefore, the pre-increment is applied to the object resulting from the*operator.So, if you wanted expanded pseudo code to explain it:
Where the last assignment could happen at any point after the first assignment.