Page 1 of 1

indirectly edioting a pointer c++

Posted: Sat Dec 18, 2004 10:23 pm
by keeper
how would i go about editing a pointer indirectly?
i want to give the pointer the address to edit at runtime.

eg. the address of 0x65456 = 15
int* addr;
cin>>addr;
*addr = 20
cout<<*addr


runtime:

input: 0x65456
output: 20

Re:indirectly edioting a pointer c++

Posted: Sun Dec 19, 2004 2:19 am
by keeper
im sorry i have put this in the wrong spot

Re:indirectly edioting a pointer c++

Posted: Fri Dec 31, 2004 2:42 am
by Adek336
Afaik, you can use the C or C++ type casting (don?t know much on the latter).

So:

Code: Select all

int * ptr;

(int)ptr= 0x66665;
cin >> (int)ptr;

cout << ?the pointer points at ? << (int)ptr << " and at that place im memory, we?ve got the int ? << *ptr << endl;

(unsigned int) ptr=0x12345678;
(char) ptr= 0x2f; //equivalent to setting (int)ptr=0x0000002f
(char)ptr=0x80; //equivalent to setting (int)ptr=0xffffff80 for bit copying- corrections welcome!
 
A pointer is 4 bytes and int or unsigned int are 4 bytes long; generally try to learn on casts.

Cheers ;D

Re:indirectly edioting a pointer c++

Posted: Mon Jan 03, 2005 4:34 am
by Solar
Adek336 wrote: A pointer is 4 bytes and int or unsigned int are 4 bytes long; generally try to learn on casts.
No, a pointer is not necessarily 4 bytes, and neither is an int.

The C99 header <stdint.h> provides the intptr_t typedef, which defines an integer that can be cast to a void *. That's the only "safe" way to play it unless you do some autoconf trickery to find out the correct integer type yourself. Unfortunately C++ doesn't define <stdint.h>.