gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > i really need help here.

#41561 - sparda - Fri Apr 29, 2005 6:24 pm

ok, i've been learning all about C programming for the last 2 weeks. But Im stuck at pointers. Now here's my question, I dont completely understand what this means :

(*(volatile unsigned short*) 0x4000000)

i understand Volatile and everything else, i just dont get the pointers, why is it

*(volatile unsigned short*) 0x4000000 why not just--> *(volatile unsigned short) 0x4000000

or maybe--> (volatile unsigned short*) 0x4000000 ??


Any help would be appreciated. Thank you.
sparda is offline Reply With Quote
_________________
genius is 1% inspiration, 99% perspiration .

#41567 - DekuTree64 - Fri Apr 29, 2005 6:43 pm

That statement just means to reference the 2-byte value at memory address 0x4000000, which the hardware translates into meaning the display control register.
Casting the raw address to volatile unsigned short* tells the compiler how to treat it, and dereferencing with the * operator says you want the value stored at the address, and not the address itself.

Wrapping #defines in parentheses is pretty standard practice, even when it doesn't make a real difference. Since the preprocessor just does a straight text replace, you want to make sure your value is as isolated from everything else as possible, so it can't be affected by any of the surrounding code where it gets plugged in.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#41569 - tepples - Fri Apr 29, 2005 6:50 pm

sparda wrote:
Now here's my question, I dont completely understand what this means :

(*(volatile unsigned short*) 0x4000000)

i understand Volatile and everything else, i just dont get the pointers, why is it

*(volatile unsigned short*) 0x4000000 why not just--> *(volatile unsigned short) 0x4000000

That doesn't work. Qualifiers such as 'const' and 'volatile' are intended for lvalues such as things pointed to; you can't add them to an rvalue such as a literal number. You may understand better after I explain the next one.

Quote:
or maybe--> (volatile unsigned short*) 0x4000000 ??

This is a valid expression for a pointer to the start of register space or specifically to DISPCNT. Adding a * in front dereferences this pointer, resulting in the value of DISPCNT.

In general, C pointer expressions are best understood from right to left like Hebrew:
  • 0x04000000 - a number
  • (unsigned short *)0x04000000 - a pointer whose bit pattern is the same as the number, which on the GBA refers to DISPCNT and the following registers
  • (volatile unsigned short *)0x04000000 - the same pointer, except the compiler will always issue write instructions in the sequence that they're given in the program
  • *(volatile unsigned short *)0x04000000 - the data contained at that address

_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#41570 - sajiimori - Fri Apr 29, 2005 6:53 pm

Edit: oops, too slow. ;)

This is basically reiterating what Deku said, but...
Quote:
why not just--> *(volatile unsigned short) 0x4000000

Because the * at the beginning means "dereference" -- which is to say, "get the value that the following pointer points to". You can only dereference pointers, so the above would not compile because the cast is telling the compiler to treat the number as a short, not as a pointer.
Quote:
or maybe--> (volatile unsigned short*) 0x4000000

That would compile, but it would only tells the compiler to treat the number as a pointer. You would still have to use * to actually get the value at that address.

So in short, the complete phrase should be read right to left as: "Take the value 0x4000000, treat it as a pointer to a volatile unsigned short, and then read the unsigned short at that address."

#41674 - sparda - Sun May 01, 2005 1:18 am

Yo, guys thanks alot for all the info, thankfully i understand it now. I really appreciate all the help. again, thanks alot.

--sparda
_________________
genius is 1% inspiration, 99% perspiration .