Re: [cc65] type conversions again...

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2002-12-20 00:43:54


On Thu, Dec 19, 2002 at 03:22:31PM -0800, Shawn Jefferson wrote:
> array[38] = (unsigned char) &array % 256;
> array[39] = (unsigned char) &array / 256;

&array is an address which can't be divided by something. So the fact that the
compiler doesn't generate an error is a sign for trouble. The precedence of
the cast is higher than that of the operator, so what you're doing here is:

    array[38] = ((unsigned char) &array) % 256;
    array[39] = ((unsigned char) &array) / 256;

This is also the reason why the compiler doesn't complain about dividing a
pointer by an integer.

Maybe you have tried this:

    array[38] = (unsigned char) (&array % 256);
    array[39] = (unsigned char) (&array / 256);

This gives a compiler error: "Integer expression expected". This error means
that you're doing the right thing, but you need to convince the compiler to
accept the division. The problem here is that you cannot divide a pointer by
something, so you have to convert this pointer into a number before doing the
divide:

    array[38] = ((unsigned) &array) % 256;
    array[39] = ((unsigned) &array) / 256;

With just a few exceptions (the sizeof operator being one) an array is
equivalent to a pointer to it's first element, so you can also write this
instead:

    array[38] = ((unsigned) array) % 256;
    array[39] = ((unsigned) array) / 256;

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2002-12-20 00:44:03 CET