Thread: precedence

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    36

    precedence

    i read in a book about c that post ++ has higher precedence than the unary dereference *.

    if that's true then why in a loop like the follwoning

    char *p, *q;
    while ( *p++ = *q++);

    *p++ isn't calculated as *(p++)?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Good question, it shows that you've been paying attention. We like that. Think about it this way:

    T val;
    T *p;

    ...

    val = *p++;

    1) The postfix increment makes a copy of p
    2) The real p is incremented
    3) The copy is dereferenced
    4) The result of the dereference is assigned to val

    So the increment is performed first, but by implementation of definition, it appears as if the indirection comes first.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    Thanx. Do you know any site or tutorial about precedence ?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here's one: C Language Operator Precedence Chart.

    [edit]
    And here's an interesting article explaining that there is no such thing as operator precedence in C.
    [/edit]
    Last edited by Dave_Sinkula; 05-22-2003 at 04:04 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have a window take z-order precedence over - Games?
    By guitarist809 in forum Windows Programming
    Replies: 2
    Last Post: 07-19-2008, 08:11 PM
  2. C4554 Warning, check operator precedence for possible error
    By Bassquake in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2008, 05:13 AM
  3. Replies: 15
    Last Post: 11-04-2006, 04:06 AM
  4. precedence / associativity
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2003, 11:03 AM
  5. Less than or equal - precedence in for loops
    By Idle in forum C Programming
    Replies: 4
    Last Post: 06-23-2003, 06:19 PM