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
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:
The correct way to make the code work is as follows: