Hi.
Why isn't it possible to 'manually' give a pointer the adress of a variable?
Example:
Code:int Integer; int * Pointer; Pointer = 0x22ff70; //Instead of: Pointer = & Integer;
Hi.
Why isn't it possible to 'manually' give a pointer the adress of a variable?
Example:
Code:int Integer; int * Pointer; Pointer = 0x22ff70; //Instead of: Pointer = & Integer;
Note: I know that people shouldn't program like this but I only wanna know How To give a pointer an adress like that, it gives compile errors.
you can not do that with any 32-bit compiler because the os prevents a program from accessing physical memory that it does not own. In 16-bit MS-DOS 6.X compilers, such as Tur bo C, that was easy to do because the program had access to all available memory below the first meg. In 32-bit os such as MS-Windows and *nix an address such as 0x22ff70 can be anywhere including the os itself or some other program that is running.
Of course you can do it
Pointer = (int*)0x22ff70;
Now whether your OS will allow you to dereference that pointer, or whether your hardware has any memory mapped at that address is another matter.
It doesn't make sense to do this in normal user-space programs on your desktop operating systems, but you see this in driver-space code and embedded systems all the time.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.