return an address of a double
i'm having an issue understanding why the following works:
void doubleAddr(double* source, double** dest)
{
*dest = source;
}
i get a pointer to a double and want to change the double that dest points to:
//usage:
int main()
{
double* num;
double* dest;
开发者_如何转开发 doubleAddr(num, &dest);
return 0;
}
thanks in advance
You're not using pointers correctly. To begin with, you're not initializing anything in main()
, so that's pretty dangerous. Your code for doubleAddr()
is correct, but it's copying a pointer, not a double. If you're trying to copy a number through a pointer, you want something like:
void copyDouble(double source, double *dest)
{
*dest = source;
}
int main()
{
double num = 5.6;
double dest;
copyDouble(num, &dest);
printf("%f\n", dest);
return 0;
}
You can add more *
or &
if you're trying to do something different.
The function works because you are not actually accessing the memory that it being pointed at. You are simply assigning the destination pointer variable to point at the same memory address as the source pointer variable, nothing more. Since your 'num' variable does not actually point at a valid double value in memory, your code will have bad behavior if you try to dereference the pointer afterwards, since it is pointing at random memory. In other words:
int main()
{
double* num; // <- uninitialized, points at random memory
double* dest;
doubleAddr(num, &dest);
// 'dest' now points to the same address that 'num' points to
*dest = 12345.0; // BAD!!!!
return 0;
}
The correct way to make the code work is as follows:
int main()
{
double num; // <- physical value in memory
double* dest;
doubleAddr(&num, &dest);
// 'dest' now points at 'num'
*dest = 12345.0; // OK! 'num' is updated correctly
return 0;
}
In your function, source
is a pointer to a double. dest
is a pointer to a pointer to a double.
*dest = source
dereferences dest
once (making it a pointer to a double) and sets it to source
.
In main()
, dest
is a pointer to a double, and you take the address of that when calling doubleAddr
, making it a pointer to a pointer to a double.
Lets take it a step at a time:
doubleAddr
works becausedouble** dest
is a pointer-to-a-pointer. This is not really the best way to do it, but is a good learning exercise.You are passing in the address of dest with
&dest
, which is then dereferenced back to a "double" pointer when you say*dest
.Since
source
is also a "double" pointer,*dest = source
just works.
By the way, your program is incomplete. If you want to test it you need to modify it, for example:
int main()
{
double* num;
double* dest;
double test;
num = &test;
doubleAddr(num, &dest);
printf("%lf\n", *dest);
return 0;
}
In the first function, source is a pointer to double, and dest is a pointer to a pointer to double, which is just so it can be modified by the function.
The assignment
*dest = source;
modifies the callers copy of dest.
And, yeah, main calls it with uninitialized data. Garbage in, garbage out.
Firstly, I am surprised that it worked - this is down to a stroke of having a 'good luck'... since the variables are pointing to anything, it worked first time, next time you may not be so lucky, worst would be the code will crash!
To put this into perspective, try this:
The calling of doubleAddr
is correct, but... you have not allocated memory for the variables num
and dest
, change around the parameters slightly as shown below.
double num = 5.0F; double dest = 0.0F; doubleAddr(num, &dest);
Then modify the parameters for doubleAddr
void doubleAddr(double source, double* dest) { *dest = source; }
Notice that dest
will have the value of '5.0', that is a call-by-reference parameter which takes the address of the variable dest
.
精彩评论