How to parse ?

Discussion in 'C Programming' started by junky_fellow@yahoo.co.in, Jun 21, 2005.

  1. Guest

    How the following expression is parsed ?

    *(int *)&flag;

    What are the general steps that should be followed in interpreting
    such expressions ?

    Thanx for any help ...
     
    , Jun 21, 2005
    #1
    1. Advertising

  2. Zoran Cutura Guest

    wrote:
    > How the following expression is parsed ?
    >
    > *(int *)&flag;
    >
    > What are the general steps that should be followed in interpreting
    > such expressions ?


    The precedence and associativity rules of the operators can be taken
    from most text books. For example K&R2s table 2-1 list them.

    You take a look at the operators and find out in which order they are to
    be evaluated. In your example above (which may result in undefined
    behavior and doesn't have have real live meaning at all) the "*", "()"
    and "&" operator all have the same precedence but are to be evaluated
    right to left according to K&R2 so it goes like.

    Get the address of flag, let that address be a pointer to an int and
    take the content of that int.

    --
    Z ()
    "LISP is worth learning for the profound enlightenment experience
    you will have when you finally get it; that experience will make you
    a better programmer for the rest of your days." -- Eric S. Raymond
     
    Zoran Cutura, Jun 21, 2005
    #2
    1. Advertising

  3. Guest

    Re: How to parse ?

    wrote:
    > How the following expression is parsed ?
    >
    > *(int *)&flag;
    >


    1. flag -- start with the identifier; in this case, flag
    2. &flag -- get the address of flag
    3. (int *)&flag -- convert the type of &flag from pointer to T
    (whatever flag's base type is) to pointer to int
    4. *(int *)&flag -- dereference the address of flag as though it were
    a pointer to int.

    Basically, you're intepreting the bit pattern stored in flag as though
    it were an integer. Note this may or may not give a meaningful or
    useful result depending on the base type of flag.

    > What are the general steps that should be followed in interpreting
    > such expressions ?
    >


    Read up on dereferences and cast expressions in your handy C reference
    manual.

    > Thanx for any help ...
     
    , Jun 21, 2005
    #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. Mitchua
    Replies:
    3
    Views:
    1,197
    Mitchua
    Jul 14, 2003
  2. Raj Mudaliar
    Replies:
    0
    Views:
    2,303
    Raj Mudaliar
    Jul 14, 2003
  3. John M. Lembo

    Parse Text File and Output to File

    John M. Lembo, Aug 1, 2003, in forum: Perl
    Replies:
    0
    Views:
    12,746
    John M. Lembo
    Aug 1, 2003
  4. Replies:
    19
    Views:
    1,171
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    206
    7stud --
    Feb 20, 2008
Loading...

Share This Page