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


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

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

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. Howard
    Replies:
    0
    Views:
    386
    Howard
    Jul 1, 2003
  2. Jakob Bieling
    Replies:
    0
    Views:
    436
    Jakob Bieling
    Jul 1, 2003
  3. Joe Seigh
    Replies:
    18
    Views:
    835
    tom_usenet
    Sep 22, 2003
  4. Matthias Kaeppler

    Dereference Adaptor

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

    NULL Pointer Dereference

    Denis Palmeiro, Jul 8, 2003, in forum: C Programming
    Replies:
    10
    Views:
    669
    Shill
    Jul 16, 2003
Loading...

Share This Page