Tuukka Toivonen wrote:

> > A cast doesn't convert the data; it just tells
> > the compiler to treat it differently.
> 
> Not exactly true. A cast _does_ convert in many cases the
> data. Just think this:
>    float f = 1.0;
>    int i;
>    i = (int)f;   // This _converts_ the data.
> 
> Generally a cast converts the data always when it makes sense
> and is simple enough (in the case above it wasn't simple enough).
> Of course if you convert from pointer to int any kind of
> converting doesn't make sense.

It depends upon what you consider to be the data. If you view the data
as the bit pattern, then yes, a cast may convert the data. If you
consider the data to be the number that the bit pattern represents,
then the cast is just telling the compiler to treat it differently.

The main issue in the original post is that for a pointer, what is
being converted is the pointer itself, not what it points to.

> To just treat the data differently it's probably best to use unions.

That only applies to the case where you consider the data to be the
bit pattern. The following aren't equivalent; the first version will
behave differently on architectures with differing byte orders.

        int i2s(int n)
        {
                union {
                        int i;
                        short s;
                } var;
        
                var.i = n;
                return var.s;
        }
        
        int i2s(int n)
        {
                return (short) n;
        }

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to