Pointer Addresses

I know how to use the dereference and addressof operators and know how to use pointer arithmetic but the one thing I'm unsure of is how to actually set the address of a pointer manually, this is something along the lines of what I would want:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
   int setAddress()
   {
      int *ptr;  // Declare pointer
      &ptr = 0x002FFF20;  // Set pointer address
      return 0;
   }
   setAddress();
   return 0;
}          


If anyone can tell me how one does set the address of a pointer in C/C++ then I'd appreciate it greatly.
1
2
3
4
5
6
7
8
9
10
11
12
int* setAddress()
{
   int *ptr;  // Declare pointer
   ptr = reinterpret_cast<int*>(0x002FFF20);  // Set pointer address
   return ptr;
}

int main()
{
   setAddress();
   return 0;
}

Thanks a lot. Exactly what I needed. :)

Edit: Just a further question: why "reinterpret_cast" and not C style casting? How would that line differ from merely using:

 
ptr = (int*)(0x002FFF20);

Last edited on
You're never quite sure what cast is being used when you use a C cast, so it's a possible source of error.
Last edited on
Topic archived. No new replies allowed.