1. Pointer Puzzle

I'm currently working through a C Programming text book as a refresher course and I'm a bit puzzled by this code.
Code:
```int main()
{
char string[] = "This is a text string";
char *s;

s = string;

while(*s)
{
putchar(*s++);
}
return(0);
}```
In itself this isn't a complicated code snippet, what I am puzzled about is the *s++. The text books states "The value stored at memory location s is read and displayed, and then memory location s is incremented." Is this right? As I understand it the unary incrementing operator ++ has a higher precedence than the dereferencing operator. Wouldn't the memory location be incremented first in this example?

As I understand it the unary incrementing operator ++ has a higher precedence than the dereferencing operator.
That is correct.

Wouldn't the memory location be incremented first in this example?
Since this is post-increment, the increment effectively happens after the dereference.

3. Where did you read that the increment operator '++' has higher precedence than the indirection operator '*'?

4. Since this is post-increment, the increment effectively happens after the dereference.
Does a post-increment imply that the increment happens after everything else in that line regardless of precedence of operators?

5. Originally Posted by itCbitC
Where did you read that the increment operator '++' has higher precedence than the indirection operator '*'?
Here's one place

C Operator Precedence Table

I imagine there are a zillion others...since the increment operator has higher precedence than the indirection operator in C.

Originally Posted by KBriggs
Does a post-increment imply that the increment happens after everything else in that line regardless of precedence of operators?
That would make too much sense.

6. So would I be right in thinking that the incrementing operator is indeed dealt with first as the order of precedence dicates and because it's a post-incrementing operator it only appears that the dereferencing operator is dealt with first? Thanks so much for taking the time to rely, it's very much apreacited.

7. Originally Posted by KBriggs
Does a post-increment imply that the increment happens after everything else in that line regardless of precedence of operators?
The order of evaluation and order in which side effects take place is unspecified. What I meant was that since the result of p++ is p, *p++ (or *(p++)) is effectively the same as *p; p++;.

So would I be right in thinking that the incrementing operator is indeed dealt with first as the order of precedence dicates and because it's a post-incrementing operator it only appears that the dereferencing operator is dealt with first?
Yes, though I would prefer to say "grouped with p" rather than "dealt with".

8. Originally Posted by MK27
Here's one place

C Operator Precedence Table

I imagine there are a zillion others...since the increment operator has higher precedence than the indirection operator in C.

That would make too much sense.

9. Originally Posted by itCbitC
I would have expected more accuracy out of that link. Per the standard, all the postfix operators have higher precedence than the unary prefix operators (pre-increment, pre-decrement, sizeof, &, *, -, ~, and !). I guess they can try to get away with it with the "right-to-left" ordering they specify.

10. Originally Posted by itCbitC
It does not contradict at all. Rather, it merely neglects to mention the postfix operators as a category, incorrectly calling some of them "primary operators" (but parenthesized expressions are primary expressions), and leaving out post-increment and post-decrement entirely.

11. Looking in the draft standard sec. 6.5, it seems to distinguish postfix operators as "primary" and prefix as "unary", meaning post- and pre- do not have the same level of precedence.

If prefix is unary it still better have precedence over *dereferencing, otherwise there would be no point to prefix incrementing:
Code:
`*++s;`

12. Originally Posted by laserlight
It does not contradict at all. Rather, it merely neglects to mention the postfix operators as a category, incorrectly calling some of them "primary operators" (but parenthesized expressions are primary expressions), and leaving out post-increment and post-decrement entirely.
IBTD, the link groups the increment and decrement operators into the unary category instead of the the primaries.
The post and pre suffixes for increment / decrement operators is merely contextual but they are still unary operators.

13. Originally Posted by MK27
Looking in the draft standard sec. 6.5, it seems to distinguish postfix operators as "primary" and prefix as "unary"
Good point of information, so that table was not incorrect in terminology after all.

Originally Posted by MK27
If prefix is unary it still better have precedence over *dereferencing, otherwise there would be no point to prefix incrementing:
They have the same level of precedence, but the grouping is inherent in the syntax.

Originally Posted by itCbitC
IBTD
Grr... what's with these abbreviations! First it was PDL, now IBTD, but at least I had better luck figuring out with the help of the Web that IBTD means "I Beg To Differ"

Originally Posted by itCbitC
the link groups the increment and decrement operators into the unary category instead of the the primaries.
The post and pre suffixes for increment / decrement operators is merely contextual but they are still unary operators.
As tabstop pointed out, notice the "right to left" grouping. Post-increment has left to right grouping. If you interpret that to be a simple omission of a note, then it follows that that table is wrong with respect to precedence because it contradicts the C standard.

14. Originally Posted by tabstop
I would have expected more accuracy out of that link. Per the standard, all the postfix operators have higher precedence than the unary prefix operators (pre-increment, pre-decrement, sizeof, &, *, -, ~, and !). I guess they can try to get away with it with the "right-to-left" ordering they specify.
I normally refer to the ANSI version of "The C Programming Language" (aka "the bible") as I don't have a copy of the standard. That link mirrored the precedence table in it when I Google'd so I posted it, though I agree that the link should have done a better job of explaining it. Moot point is that given an expression like *s++ what is the implied parenthesization (*s)++ or *(s++)?? The former increments the value of the accessed object while the latter increments the pointer after accessing the object it points to.

15. Originally Posted by itCbitC
I normally refer to the ANSI version of "The C Programming Language" (aka "the bible") as I don't have a copy of the standard.
I'm just using the free "draft copy" of the standard (google ISO_IEC9899.pdf); which my earlier interpretation of that would seem to agree with tabstop "Per the standard, all the postfix operators have higher precedence than the unary prefix operators".

Neither of the two links really reflects that. The first one puts post and pre together with the primary operators, the second puts them together with the unary operators.