in tutorial of pointers i found out that a pointer actually contains the "id of memory cell"
how can i get that number? and store it in double variable?
for example i have:
1 2
double* pointer=newdouble;
*pointer=5;
now that pointer points to some place where double 5 is stored,
next i am tring to do is get that number where 5 is out of my pointer and store it into another double variable, i tried like this:
Do not do this. If you want to obtain the address of poniter, use the address-of operator on poniter. If you want the address of the double pointed-to by poniter, do not dereference it, and do not use the address-of operator. For instance:
1 2 3 4 5
// Get the address of the pointer:
&some_pointer_;
// Get the address of the pointed-to object:
double *another_pointer_(some_pointer_);
Storing it in a double seems a bit silly, since it is an integer, but sure. This involves some horrific casting, but if you've already decided to represent a memory address as a double, that bridge has been burnt :)
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
usingnamespace std;
int main()
{
double* poniter=newdouble;
int number = (int)poniter;
double numberAsDouble = number;
cout << number << " " << numberAsDouble;
}
Framework, I'm interested as to why you should not do this? Aren't pointers in C++ just integers, the addresses? I'm not so hot on classical memory pointers...
thanks a lot guys, particularly to Moschops, that is exactly what i needed.
but am still curious why this works: int number = (int)poniter;
and why not this: double number = (double)pointer;
and i have just one more question,
i needed this pointer as int, beacause i am trying to export it to another program, (i am building a dll), and i am not shore how long will that memory stay there? the one that is pointed by this pointer? all untill a dll is freed from program using it?
or is this completly wrong way to do something like that?
Maybe it's because by your logic, THIS should work:
double number = (int) pointer;
The other casting was implied, but how should the compiler know to make it an int first?
EDIT: And yes, that works. You were right, but still expecting too much out of the compiler. A double is a complex number type, an address is a type of int. Connect the dots for the compiler.
"Aren't pointers in C++ just integers, the addresses?"
I guess you could say that since addresses are represented in hexadecimal. However, a double shouldn't represent an address, and trying to do so is absurd - there's no 1.2 address in memory. Also, since floating-point types struggle with rounding, you'd have a hard time getting the correct address.
And why he/she's trying to store an address in a double in the first place is doesn't make any sense. Addresses are whole numbers, so store them in integral types.
Want to know the only good way of storing an address? In a pointer! Or if that's too specific for you just stick to a void *. EDIT: I wonder if cout will output your address after casting to a void *?
I have no idea why anyone would want to store the address any other way, it's still an address; all you can accomplish is making your code platform-specific through bad data sizes.
i wasn't asking anything like that!!, noone needed to know why store it like this, we figure it out without that ok? where is the problem? if you really need to know so bad it is becaouse the progam in wich i want to export that pointer, does not accpts nothing else than strings and numbers in int double long char, ok? satisfied? in diffrent circumstances you need difrent measures ok? i never asked what is the best way to store pointer!! so answer on your first question if you haven't figure it out yet, NO I DONT WANNA KNOW.