Page 1 of 1

editing a c++ pointer indirectly

Posted: Sun Dec 19, 2004 2:20 am
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:editing a c++ pointer indirectly

Posted: Sun Dec 19, 2004 1:36 pm
by aladdin
not sure, but try with a scanf enstead of cin, remember that if u modify pointer address u can get a seg fault. ;)

Re:editing a c++ pointer indirectly

Posted: Sun Dec 19, 2004 5:41 pm
by zloba
and what are you trying to accomplish?
so you enter some arbitrary address and read/write the value there.
-you may overwrite some arbitrary piece of data and god knows what will happen.
-you may hit an unallocated memory area and the program will crash.

Code: Select all

int* addr;
cin>>addr;
*addr = 20
cout<<*addr
an address has any paricular meaning only within a particular process where it was obtained, and only when you point it to something.

Re:editing a c++ pointer indirectly

Posted: Sun Dec 19, 2004 5:50 pm
by keeper
what i want to do is something like this.

int* whatever = 0x89456;

but this wont work.

Re:editing a c++ pointer indirectly

Posted: Sun Dec 19, 2004 6:07 pm
by zloba
you need to cast it.

Code: Select all

int* whatever = (int *)0x89456;

Re:editing a c++ pointer indirectly

Posted: Tue Dec 21, 2004 12:13 am
by keeper
ohh...ok ill try this