Why not? Since when has casting from a pointer type to an
integral type been forbidden?
That's like saying that casting from long to int is
forbidden because sizeof(long) might be larger than
sizeof(int). (Of from double to float.)
[/QUOTE]
No. The standard says you can cast from long to int, even if
int is smaller than long. The standard says that you cannot
cast from pointer to int if int is smaller than a pointer.
okay, tell me does this program gives 10 on each compiler..
As has been already stated: it doesn't even compile on all
compilers. Where it compiles, it has undefined behavior (unless
the implementation has explicitly defined the behavior).
and about converting pointer-int and back from int-pointer..
is that the value is same to 10..
Pointer to int to pointer isn't the same thing as int to pointer
to int. Your code does the latter.
on this link..
http://en.allexperts.com/q/C-1040/2009/3/convert-pointer-int.htm
"A pointer can be explicitly converted to any integral type large
enough to hold it.
The mapping function is implementation defined
[Note: it is intended to be unsurprising to those who know the
addressing structure
of the underlying machine. ]"
"A value of integral type or enumeration type can be explicitly
converted to a pointer.
A pointer converted to an integer of sufficient size (if any
such exists on the
implementation) and back to the same pointer type will have its
original value;
mappings between pointers and integers are otherwise
implementation defined."
what it says in second paragraph..
Just read what it says. It specifically requires that the
integral type be large enough to hold the pointer in order to
convert to int. That's rarely the case on modern machines. And
it requires that pointer->int->pointer result in the same value
if the original pointer is valid. Which is totally irrelevant
to the code you posted.