Thread: Pointer Puzzle

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by conradsmith
    As I understand it the unary incrementing operator ++ has a higher precedence than the dereferencing operator.
    That is correct.

    Quote Originally Posted by conradsmith
    Wouldn't the memory location be incremented first in this example?
    Since this is post-increment, the increment effectively happens after the dereference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Where did you read that the increment operator '++' has higher precedence than the indirection operator '*'?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    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.

    Quote Originally Posted by KBriggs View Post
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    2
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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++;.

    Quote Originally Posted by conradsmith
    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".
    Last edited by laserlight; 06-15-2009 at 08:18 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MK27 View Post
    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.
    Here's one that contradicts what's posted in the link above.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by itCbitC View Post
    Here's one that contradicts what's posted in the link above.
    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. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    Here's one that contradicts what's posted in the link above.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    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. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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.

    Quote 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.

    Quote 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"

    Quote 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.
    Last edited by laserlight; 06-15-2009 at 09:35 AM. Reason: association -> grouping, to follow the corresponding table
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by tabstop View Post
    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. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itCbitC View Post
    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.

    I can't believe I care about this...it is definitely a purely theoretical issue, at best.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM