I'm fairly new to C and are trying to write some basic applications with a seven segment display. When declaring the absolute address, for an 8-bit port I could write something like:
typedef char *port8ptr;
#define OUT_ADR 0x400
#define OUT *((port8ptr) OUT_ADR)
and then simply write to the variable out something like
OUT = 0x80;
to get Hexadecimal 80 to the port. But exactly what does the code above mean? That is, why does one define a pointer (line one) and then cast the address to be a pointer to a pointer (?!) ? It obviously works, but I don't really like using code from an example I can't understand.
Another way they did the type conversion was with the line
((unsigned char *) 0x400)
But I honestly don't get that either.
Many thanks in advance!
Axel
When you do all the preprocessing you get this line:
*((char*)0x400) = 0x80;
Let's dissect that. (char *)0x400
means take the number 0x400
and cast it to a "pointer-to-a-char". Basically it says here: let's create a pointer that points to address 0x400
.
Then, we take that *
in front of which means "dereference", making you can actually write something in the memory spot that the pointer points to, in this case address 0x400
. And then you write 0x80
in it.