cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : Pointer question
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  Pointer question

chris (16)
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.
|
SteakRider (110)
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
chris (16)
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).
|
jsmith (400)
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....
|
Incubbus (28)
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...
|
chris (16)
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

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us