dereference precedence

Discussion in 'C Programming' started by Toni, Apr 13, 2006.

  1. Toni

    Toni Guest

    Hi, This has probably debated here before but I could not find it in Google.

    Though I don't have the standard at hand I've seen several references
    point that operator -> has higher precedence than unary * (dereference).

    Then I would always have thought that

    *a->b

    Is equivalent to

    (*a)->b

    but apparently it should be

    *(a->b)

    Which which is right and why?

    Thanks,

    Toni
     
    Toni, Apr 13, 2006
    #1
    1. Advertisements


  2. > Though I don't have the standard at hand I've seen several references
    > point that operator -> has higher precedence than unary * (dereference).

    That's right...

    >
    > Then I would always have thought that
    >
    > *a->b
    >
    > Is equivalent to
    >
    > (*a)->b

    No, if this is the case then * has a higher precedence that -> which
    contradict what you said above.
    Also this is meaningless, it should be (*a).b

    >
    > but apparently it should be
    >
    > *(a->b)
    >
    > Which which is right and why?

    The second one

    > Thanks,

    Welcome..

    Abdo Haji-Ali
    Programmer
    In|Framez
     
    Abdo Haji-Ali, Apr 13, 2006
    #2
    1. Advertisements

  3. > >
    > > (*a)->b

    > No, if this is the case then * has a higher precedence that -> which
    > contradict what you said above.
    > Also this is meaningless, it should be (*a).b


    Silly me, I just assumed that 'a' is a one-level pointer, which is not
    necessarily... Sorry

    Abdo Haji-Ali
    Programmer
    In|Framez
     
    Abdo Haji-Ali, Apr 13, 2006
    #3
  4. Toni

    Toni Guest

    En/na Abdo Haji-Ali ha escrit:
    >>> (*a)->b

    >> No, if this is the case then * has a higher precedence that -> which
    >> contradict what you said above.


    This is just what I thought, it was just one of those occasions where
    the feelings contradict the logic. As (nearly) always the logic turns
    out to be right.

    Thanks

    Toni
     
    Toni, Apr 13, 2006
    #4
  5. Toni

    Bill Pursell Guest

    Abdo Haji-Ali wrote:
    > > Though I don't have the standard at hand I've seen several references
    > > point that operator -> has higher precedence than unary * (dereference).

    > That's right...
    >
    > >
    > > Then I would always have thought that
    > >
    > > *a->b
    > >
    > > Is equivalent to
    > >
    > > (*a)->b

    > No, if this is the case then * has a higher precedence that -> which
    > contradict what you said above.
    > Also this is meaningless, it should be (*a).b


    (*a)->b is not necessarily meaningless.

    #include <stdio.h>

    int
    main(void)
    {
    struct foo {
    int b;
    } c[1];

    struct foo *a[1];

    c[0].b = 3;
    a[0] = c;

    printf("%d\n", (*a)->b);

    }
     
    Bill Pursell, Apr 13, 2006
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Seigh
    Replies:
    18
    Views:
    1,037
    tom_usenet
    Sep 22, 2003
  2. Matthias Kaeppler

    Dereference Adaptor

    Matthias Kaeppler, Feb 27, 2005, in forum: C++
    Replies:
    3
    Views:
    606
    Kurt Krueckeberg
    Feb 28, 2005
  3. Denis Palmeiro

    NULL Pointer Dereference

    Denis Palmeiro, Jul 8, 2003, in forum: C Programming
    Replies:
    10
    Views:
    873
    Shill
    Jul 16, 2003
  4. TuxC0d3
    Replies:
    6
    Views:
    1,902
    John Carson
    Dec 6, 2005
  5. Replies:
    9
    Views:
    766
    Bo Persson
    Feb 11, 2006
  6. somenath

    pointer dereference

    somenath, Jul 12, 2007, in forum: C Programming
    Replies:
    34
    Views:
    1,184
    Anurag
    Jul 18, 2007
  7. somenath

    pointer dereference

    somenath, Aug 9, 2007, in forum: C Programming
    Replies:
    12
    Views:
    851
    Martin Ambuhl
    Aug 10, 2007
  8. Replies:
    6
    Views:
    626
    Alf P. Steinbach
    Oct 3, 2007
Loading...