EDIT: oops the code is running fine but the program is keep crashing.
i done it once when i was learning about pointers but now i forgot how to do it. i know it should not give any error.
i tried your code but it's keep crashing and i would like to show you the code i am trying to run.
1 2 3 4 5 6 7 8 9 10
#include<conio.h>
#include<stdio.h>
int main()
{
int x=9,*p;
p=(int*)0x2293528; //lets think that the variable x i declared is stored in given adderss
printf("%d",*p);
return 0;
}
int* p pointer to int 0x229352A memory address (hex value) (int*) convert to int pointer (location)
this is same as: int* p = static_cast<int*>(0x229352A);
here new operator returns memory addres (hex value) and assign it to int pointer int* p = newint
so there is no diffrerence will you do that with new operator or manulay assing an address.
Last time I tried, a reinterpret_cast<> was needed.
yeah, you're right.
that why "C stile" casting always success,
cos (int*) is static_cast and reinterpret_cast in same time, when staic does not work then it uses reinterpret.
You do know that every time you run this, you're not guaranteed to have 'x' stored in the same memory location? Address 0x2293532 could belong to any running process, not just yours. It's also likely to be unallocated.
If you want to access the memory where x is stored, use
1 2 3
int x = 8;
int* p = &x; // Address of x
printf("%d", *p);
i know that the address is keep changing.
to prevent that kind of error i first check the address of the variable stored in and after that i use the pointer to get the data from there.
the example you given above is something i know.
i want to do this because i want to learn how to do it and i need it.