( ) -> operators associativity confused

Discussion in 'C Programming' started by bochengnever@gmail.com, Oct 25, 2006.

  1. Guest

    ( ) and -> are left to right in the same order . eg:
    struct foo
    {
    int a ;
    void * p;
    }

    main()
    {
    struct foo* A= malloc(sizeof(struct foo));
    (char*)A->p;
    }
    (char*)A->p I think should be a compiler error,because (char*)A evalute
    first ,now A come be a pointer to char ,so A do not have a field of p .
    But it compiled successfully.So A->p first evaluted,
    it seem to conflict to the associativity of () and -> .

    thank you
    , Oct 25, 2006
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:

    > ( ) and -> are left to right in the same order . eg:
    > struct foo
    > {
    > int a ;
    > void * p;
    > }
    >
    > main()
    > {
    > struct foo* A= malloc(sizeof(struct foo));
    > (char*)A->p;
    > }
    > (char*)A->p I think should be a compiler error,because (char*)A evalute
    > first ,now A come be a pointer to char ,so A do not have a field of p .
    > But it compiled successfully.So A->p first evaluted,
    > it seem to conflict to the associativity of () and -> .


    -> has higher precedence than ("binds more tightly than")
    (type). Associativity is not by itself a complete description
    of the syntax.

    In fact, both precedence and associativity are just verbal
    conveniences. The language's syntax is defined by a BNF grammar
    that parses `(char*)A->b' as

    cast-expression
    ::= ( type-name ) cast-expression
    ::= ( char* ) cast-expression
    ::= ( char* ) unary-expression
    ::= ( char* ) postfix-expression
    ::= ( char* ) primary-expression -> identifier
    ::= ( char* ) identifier -> identifier
    ::= ( char* ) A -> identifier
    ::= ( char* ) A -> b

    In the transition from the first line to the second, you can see
    that `(char*)A->b' divides into the two pieces `(char*)' and
    `A->b', not into `(char*)A' and `->b'.

    --
    Eric Sosman
    lid
    Eric Sosman, Oct 25, 2006
    #2
    1. Advertising

  3. wrote:
    > (char*)A->p I think should be a compiler error,because (char*)A evalute
    > first ,now A come be a pointer to char ,so A do not have a field of p .
    > But it compiled successfully.So A->p first evaluted,
    > it seem to conflict to the associativity of () and -> .
    > ...


    I don't know where you found such strange "associativity", but normally in C
    postfix operators have higher priority than prefix operators, meaning that
    '(char*) A->p' is interpreted as '(char*) (A->p)' and, therefore, should compile.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Oct 25, 2006
    #3
    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. Mantorok Redgormor

    associativity

    Mantorok Redgormor, Jan 20, 2004, in forum: C Programming
    Replies:
    2
    Views:
    361
    Barry Schwarz
    Jan 21, 2004
  2. Ashwin  N

    Associativity of unary operators

    Ashwin N, Sep 8, 2006, in forum: Java
    Replies:
    4
    Views:
    1,720
    Stefan Ram
    Sep 9, 2006
  3. maadhuu
    Replies:
    3
    Views:
    352
    maadhuu
    Jul 29, 2005
  4. dspfun

    Associativity of unary C Operators

    dspfun, Jan 1, 2007, in forum: C Programming
    Replies:
    28
    Views:
    883
    CBFalconer
    Jan 5, 2007
  5. , India

    associativity of operators

    , India, Mar 16, 2007, in forum: C Programming
    Replies:
    8
    Views:
    324
    Richard Harter
    Mar 18, 2007
Loading...

Share This Page