You know, It's not that I don't understand what a pointer is... it's just an address, but there is something mind-numbing dumb about how C manages them that just makes me want to strangle Dennis Ritchie if it wasn't for the fact he's already dead.
Ok, I want a block of 32k that I can reference with a pointer. I have extra ram in my Mega do I want the space way at the bottom from 0x8000 to 0xFFFF
First thing's first, let's define the pointer and point it to the top of my block.
uint8_t *memory = 0x8000;
and I get a compile-time error. "error: invalid conversion from 'unsigned int' to 'uint8_t*'"
Huh? where is it getting the unsigned int from? I explicitly told it I wanted an unsigned 8 bit int as a pointer. Now, I don't know why it's doing this, but I know how to fix it, which an unacceptable solution. I want to know what's going on and not blindly guessing how how my own code is functioning. That's just bad coding.
anyway, the solution to the problem is to define the pointer with a cast... I think. It looks like this.
uint8_t * memory = (uint8_t *)0x8000;
I have no idea what this is doing. I can assume it's a cast because of the parenthesis. But what am I casting? From what to what? That figging star makes no sense.
Let me take a step back so I can make sure I have pointers and casts right.
- = "address of" meaning that that you read "*foo=100" as "The address of foo is 100"
& = "value at address of" meaning that you read "&foo=16" as "The value at the address of foo is 16"
Taking this my initial deceleration of {uint8_t *memory = 0x8000;} reads thusly.
"I want to to make a pointer called "memory" where each cell is an unsigned unsigned 8-bit integer wide, and put the pointer at memory location 0x8000"
But that doesn't work. My new declaration {uint8_t * memory = (uint8_t *)0x8000;} reads like this..
"I want to to make a pointer called "memory" where each cell is an unsigned unsigned 8-bit integer wide, and then convert 0x8000 into the address of an unsigned 8-bit integer wide pointer." (Which is dumb because it's the same memory location)
I don't understand this. I'm assigning an explicit address why should the compiler "care" that type it is.
Since when do addresses have "types" other than "this location"
Now, to stem off an further issue, after I get this whole memory-pointer thing straightened out I will want to create functions that put data into a particular memory location, and take data out.
void memStoreByte(int addr,uint8_t value ) {
memory = address;
&memory = value;
}
uint8_t memReadByte(int addr ) {
memory = address;
return &memory;
}
I have a feeling these are going to be incorrect because of the cast... Can you point out the error of my ways?