How to parse ?

J

junky_fellow

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

Zoran Cutura

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

john_bode

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top