spacebaby
asked on
Accessing a specific memory address
I am trying to get a handle on stand alone pc programming, and I need to access a specific address in memory. I have a program that reads in a string representation of an address in hexidecimal. I want to be able to read from or write to that address. So, how do I declare a pointer to that address after converting the string representation to an integer with sscanf? I have looked through my c programming manual and all over online for a simple line of code that explains how to do this, but I have had no luck. All I can find are examples of how to declare a pointer and assign the address of a variable to it.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sunnycoder,
what is printf ("%p\n",b);
regards,
preetham
what is printf ("%p\n",b);
regards,
preetham
from the man page again
p The void * pointer argument is printed in hexadeci
mal (as if by %#x or %#lx).
p The void * pointer argument is printed in hexadeci
mal (as if by %#x or %#lx).
ASKER
Okay... just a few more questions. What is a "char far" cast? That type of declaration isn't in my C manual either. I also should say that I am working in two environments -- unix for one version of this program and a stand alone PC environment that uses the intel processor for the other version. I haven't ever used the stroul function before, and my professor wants us to use sscanf to convert the string, so I should do what he says.
So, to recap from various comments, how would this work?
#include <stdio.h>
#include <string.h>
int convert(char *argument, char far * pointerToAddress) /* argument is the string representation of the address */
{
unsigned int address;
if (sscanf(argument, "%u", &address) == 1) /* Is the argument valid? */
{
pointerToAddress = (char far *) address;
}
}
So, to recap from various comments, how would this work?
#include <stdio.h>
#include <string.h>
int convert(char *argument, char far * pointerToAddress) /* argument is the string representation of the address */
{
unsigned int address;
if (sscanf(argument, "%u", &address) == 1) /* Is the argument valid? */
{
pointerToAddress = (char far *) address;
}
}
far pointers were used in the days of 16 bit DOS programming ... they are now obselete and 32 bit newer OSes do not use them
so your code gets modified to something like this
#include <stdio.h>
int main()
{
char * a="0xAB88";
char * b =0 ;
sscanf (a,"%p",&b );
printf ("%p\n",b);
}
I have tested it and it works
so your code gets modified to something like this
#include <stdio.h>
int main()
{
char * a="0xAB88";
char * b =0 ;
sscanf (a,"%p",&b );
printf ("%p\n",b);
}
I have tested it and it works
>int convert(char *argument, char far * pointerToAddress) /* argument is the string representation of the address */
since you want pointerToAddress to actually hold that value, it will be a good idea to pass the address of pointerToAddress
char **, since you will be needing that in sscanf
since you want pointerToAddress to actually hold that value, it will be a good idea to pass the address of pointerToAddress
char **, since you will be needing that in sscanf
If you decide to stick with sscanf, all you need to do is to modify the above code to get value using sscanf instead of strtoul