views:

75

answers:

6

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

+4  A: 

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.

Carl Norum
you are corrent, i didn't use pointers correctly, but that wasn't my intention. i really wanted just to assign source's address to dest's. thank you
bks
+1  A: 

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.

John at CashCommons
cool. thank you
bks
+1  A: 

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; 
} 
Remy Lebeau - TeamB
thank you. the problem in my sample was that i didn't actually pointed to doubles, was in a bit of a hurry i guess... anyway, you are the chosen one :) your answer is the fullest
bks
A: 

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.

Jamie Cox
true, garbage problem is there, i'll clean it up. thank you for your time
bks
+1  A: 

Lets take it a step at a time:

  • doubleAddr works because double** 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;
}
Justin Ethier
nice. step by step works for me. well, i was in a bit of a hurry and forgot to make the pointers point at something. your answer made things even clearer thank before. thank you
bks
A: 
tommieb75
i tryed to pass the address itself, not to change the value. in my project we get a pointer to a struct and need to return the address of one of its fields. that's why i used double**. thank you for your time
bks