editing a c++ pointer indirectly

Programming, for all ages and all languages.
Post Reply
keeper

editing a c++ pointer indirectly

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
aladdin

Re:editing a c++ pointer indirectly

Post 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. ;)
zloba

Re:editing a c++ pointer indirectly

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

Re:editing a c++ pointer indirectly

Post by keeper »

what i want to do is something like this.

int* whatever = 0x89456;

but this wont work.
zloba

Re:editing a c++ pointer indirectly

Post by zloba »

you need to cast it.

Code: Select all

int* whatever = (int *)0x89456;
keeper

Re:editing a c++ pointer indirectly

Post by keeper »

ohh...ok ill try this
Post Reply