I haven't been able to find a full solution to this issue yet, so I started an account here in the hope that I could get some help. I'm not a frequent C programmer, so if I've made some fundamental mistakes then I apologize.
I'm trying to set up a portion of my program that can read a value from a particular memory address. However, I want the user (me) to be able to modify the memory location being read, so I can access various memory segments at will. In other words, I tell the software what address I want, and it either modifies the value at that memory location or it spits out the value stored at that memory location.
It's easy in Assembly; I just dump the desired address into the index register and then do my thing, but I can't seem to make the pointers work out right in C.
Using my awesome knowledge of pointers, I tried this, but it failed miserably:
Code:
void partOfMyCode(void)
{
unsigned long a1;
unsigned long v1;
a1 = 0x0000C762;
v1 = *a1;
}
I really, honestly thought this would work, because I thought that the * operator generated a reference to the following address. So, then I tried this, thinking it was something with the way the compiler was interpreting my code:
Code:
void partOfMyCode(void)
{
unsigned long v1;
v1 = *0x0000C762;
}
I was certain that was going to work, because the compiler couldn't possible be misunderstanding me. Nope. More failure. So, I did as much research as I could, and now I can do this, where I hard-code the address at design-time, and it works:
Code:
void partOfMyCode(void)
{
unsigned long *p1;
unsigned long v1;
p1 = (unsigned long *)0x0000C762;
v1 = *p1;
}
However, this doesn't work:
Code:
void partOfMyCode(void)
{
unsigned long *p1;
unsigned long a1;
unsigned long v1;
a1 = 0x0000C762;
p1 = (unsigned long *)a1;
v1 = *p1;
}
In the long run, as I mentioned, I want to be able to adjust the value of a1 on the fly through a user input (which just takes an integer argument), and then have the software either write to or read from that specific address. In other words, at some point I want to have
Code:
{
a1=someUserInput;
{working code here}
someUserOutput = valueAtA1;
}
Any help is much appreciated.
Matt