Raavgo Raavgo - 2 months ago 15
C Question

Switch value of variables using double pointers

I got a problem with my pointer program, it should use double pointers to switch the value of double values x and y. I am pretty new to pointers and I am not able to accomplish that.

#include <stdio.h>
#include <math.h>

/*
todo
implement switch_pp(**a,**b)
switch the content of a,b using double pointers
Only switch the values all pointer addresses must remain the same!
*/

void switch_pp(double** a,double** b){
double valueOfA = **a;
double valueOfB = **b;
*a = &valueOfB;
*b = &valueOfA;
}

int main(int argc, char* argv[]){

double x = 14.435,y = 4.435; //needs to work with these parameters
double *ptr_x = &x, *ptr_y= &y;

printf("Swapping x and y\n");
printf("X -> address: %p | value: %f || Y-> address: %p | value: %f\n", x,x,y,y);
switch_pp(&ptr_x,&ptr_y);
printf("Done swapping x and y\n");
printf("X -> address: %p | value: %f || Y-> address: %p | value: %f\n", x,x,y,y);
return 0;
}


The output is:

Swapping x and y
X -> address: A3D70A3D | value: -0.000000 || Y-> address: 4011BD70 | value: 4.435000
Done swapping x and y
X -> address: A3D70A3D | value: -0.000000 || Y-> address: 4011BD70 | value: 4.435000

Process returned 0 (0x0) execution time : 0.016 s
Press any key to continue.


Okay now I rewrote my code still the values of x,y won 't swap! although I reference *a = &ValueOfB; But as far as I understood &ptr_x -> ptr_x -> x so if I write *a = &ValueOfB I could also substitute *a with ptr_x so I can further substitute to ptr_x = &y! which should work

Answer

What you had orignally doesn't work because you pass a double * to a function that expects a double **. The types aren't compatible, and attempting to use them that way results in undefined behavior.

With your edit, it still won't work because ptr_x and ptr_y are modified to contain the address of the variables valueOfA and valueOfB inside of switch_pp. When the function returns, those variables are out of scope, so attempting to dereference those pointers again results in undefined behavior.

You would only need a double pointer (i.e. a pointer to a pointer) if you are swapping pointers. But you aren't swapping pointers, you're swapping values of type double. So just pass the addresses of each and swap what each one points to:

void swap(double *a, double *b)
{
    double tmp = *a;
    *a = *b;
    *b = tmp;
}

Then call it like this:

swap(&a, &b);

EDIT:

If you really need to pass a pointer-to-pointer, then do this:

void swap(double **a, double **b)
{
    double tmp = **a;
    **a = **b;
    **b = tmp;
}

EDIT2:

The addresses are not changing, but it might appear that way because you're not printing them correctly:

printf("X -> address: %p | value: %f || Y-> address: %p | value: %f\n", x,x,y,y);

Here, you're passing a double to the %p format specifier which expects a void *. You want this instead:

printf("X -> address: %p | value: %f || Y-> address: %p | value: %f\n", (void *)ptr_x,x,(void *)ptr_y,y);

Note that this is one of the rare time you actually need to cast a pointer to a void *.

Comments