Velocity Reviews > How to parse ?

# How to parse ?

junky_fellow@yahoo.co.in
Guest
Posts: n/a

 06-21-2005
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 ...

Zoran Cutura
Guest
Posts: n/a

 06-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 ((E-Mail Removed))
"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

john_bode@my-deja.com
Guest
Posts: n/a

 06-21-2005

(E-Mail Removed) 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 ...