indirectly edioting a pointer c++

All off topic discussions go here. Everything from the funny thing your cat did to your favorite tv shows. Non-programming computer questions are ok too.
Post Reply
keeper

indirectly edioting a pointer c++

Post 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
keeper

Re:indirectly edioting a pointer c++

Post by keeper »

im sorry i have put this in the wrong spot
Adek336

Re:indirectly edioting a pointer c++

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:indirectly edioting a pointer c++

Post 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>.
Every good solution is obvious once you've found it.
Post Reply