Pointer question

May 5, 2008 at 7:47am
All I basically want to do is have a memory address (for example 0x22FF74), & be able to use that to display the value stored in it.
I know how to do like:
1
2
int x=1;
printf("0x%X",&x);

Using the & reference operator to get the actual address, I just don't know how to take the address & find the value from that.
Any help at all is much appreciated.
Something that could be like:
Enter your memory address: 0x22FF74
The value in 0x22FF74 is: (value)
Thanks.
May 5, 2008 at 9:01am
It is "*" operator ,

1
2
3
	int x=1;
        printf("0x%X",&x); // address of x
	printf("%d",*(&x)); // data of address(&x)                          


and

I recommend you to read this atricle
http://www.augustcouncil.com/~tgibson/tutorial/ptr.html
Last edited on May 5, 2008 at 9:01am
May 5, 2008 at 9:16am
Maybe I should have been more specific (I thought I was).
I know how to do it with variables that have been declared in the actual code, what I want to do is being able to access ANY variable using it's hexadecimal (or decimal) reference. For example using the address 0x22FF74 to find it's value, so as opposed to using *(&x) you would use like *(&0x2FF74) (that does not actually work however).
May 5, 2008 at 11:22am
1
2
unsigned char* ptr = static_cast<unsigned char*>( 0x2FF74 );
printf( "%hhu", *ptr );


gets you the 1 byte stored at that memory location.
I hope the memory location is readable otherwise you'll seg fault / GPF....
May 5, 2008 at 1:59pm
what you are trying -i guess- is, that the programm asks for an adress, you enter it, and then it puts out the dato, stored there?!?...

(i usually use the decimal usage with the iostream classes... you are able to change that on your own i think^^)

1
2
3
4
5
6
printf("Please Enter an Address");
scanf("%X", &adress);

ptr = (typeofpointer*)adress;

printf("The Value of %X is %d", adress, *ptr);


but if you try to access unreadable memory, you will get an exception...
May 5, 2008 at 3:39pm
Thanks for the help - after going through your help I got this code that does what I wanted.
1
2
3
4
5
6
7
8
9
#include<stdio.h>
int main()
{
int x=2;
int* ptr=(int*)0x22FF74;
printf("%d",*ptr);
getchar();
return 0;
}
Last edited on May 5, 2008 at 3:40pm
Topic archived. No new replies allowed.