We help IT Professionals succeed at work.

Accessing a specific memory address

spacebaby
spacebaby asked
on
532 Views
Last Modified: 2010-04-15
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.  
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2006

Commented:
I would recommend that you use strtoul rather than sscanf .... strtoul fits the bill better ... It is safer and provides with good error checking capabilities

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
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
sunnycoder,
what is     printf ("%p\n",b);
regards,
preetham
 
CERTIFIED EXPERT
Top Expert 2006

Commented:
from the man page again
       p      The void * pointer argument is printed in hexadeci¬≠
              mal (as if by %#x or %#lx).

Author

Commented:
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;
        }
}
 
CERTIFIED EXPERT
Top Expert 2006

Commented:
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
CERTIFIED EXPERT
Top Expert 2006

Commented:
>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
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.