I just have fun with pointers a bit and found that i can actually hold some variable address as value in another non-pointer variable, never seen an example of this before. Now i am wondering how are memory addresses stored, i mean, can they be negative?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
typedefunsignedlongint ulong;
int main()
{
ulong number = 10;
ulong address = (ulong)&number;//hold number address as value
std::cout << "number value is " << number << std::endl;
ulong* ptr = (ulong*)address;//set to point at that address
*ptr = 11;//alter value at address
std::cout << "number value is " << number << std::endl;
std::cout << "Press enter to exit..." << std::endl;
std::cin.get();
return 0;
}
Pointers are almost always 'considered' as unsigned. Remember, though, that bits are exactly the same signed or unsigned, so your question 'can memory addresses be negative' makes no sense.