Re: [cc65] c question

From: Marc 'BlackJack Rintsch <marc1rintsch.de>
Date: 2010-05-27 11:04:08
On Thursday 27 May 2010, Chiron Bramberger wrote:
> typedef unsigned char byte;
>      typedef unsigned word;
>      byte B;
>      word W;
>
>      *(byte*) 0xD800 = 0x12;    /* Store a byte to address $D800 */
>      *(word*) 0xC000 = 0x1234;  /* Store a word to address $C000 */
>      B = *(byte*) 0xD800;       /* Read a byte from address $D800 */
>      W = *(word*) 0xC000;       /* Read a word from address $C000 */
>
> I'm sorry, but I looked this up in my old C textbook, and even though
> there are similar examples, I can't find an answer to this question
> anywhere.
>
> What does the asterisk * in (byte*) mean? I know *p is a pointer, and
> I know &q would be an address, but what does it mean when the * is
> after something?

The ``*`` within the perenthesis means "pointer".  The syntax denotes a 
cast and says: "Cast the following value to a ``byte`` pointer."  
Without the ``*`` it would be a cast to a ``byte`` value -- not what is 
wanted here.

"``*p`` is a pointer" is a bit misleading.  I guess you mean ``p`` is a 
pointer.  ``*p`` is then dereferencing the pointer.

> typedef unsigned char BYTE;
> BYTE *p;
> p = (BYTE *) 0x1000; /* p contains address 0x1000 */
>
> I know in this case, the *p is the pointer of type BYTE, and I know
> that (BYTE *) is a cast (right?), and (in the first example) I know
> that the * on the outside to the left makes the enclosing brackets
> resolve to a pointer,  but what is the * for, and why is it inside
> the brackets and on the right?

Here again you get some things wrong.  ``*p`` is a value of type 
``BYTE`` because it is dereferencing ``p`` -- a pointer of type 
``BYTE``.

In the first example the ``*`` outside the brackets does nothing to the 
perenthesis or what's in there but to the whole expression right of it.  
Let's introduce some extra parenthesis to make that more clear::

  B = *((byte*) 0xD800);

The leftmost ``*`` resolves nothing to a pointer but dereferences the 
pointer right of it.  The expression can be read right to left.  It 
starts with ``0xD800`` which is an integer literal.  This is cast to a 
pointer of type ``byte`` with ``(byte*)``.  And finally this pointer is 
dereferenced, yielding a ``byte`` value which is stored in ``B``.

> I know 0x1000 is a literal verbatim and explicitly stated memory
> address, 

Pendantically speaking it's just a literal ``int`` value.  The cast to a 
pointer type makes it a memory address.

Ciao,
	Marc 'BlackJack' Rintsch
-- 
GOD is REAL … unless declared INTEGER

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Thu May 27 10:59:14 2010

This archive was generated by hypermail 2.1.8 : 2010-05-27 10:59:17 CEST