views:

465

answers:

7

How do you switch pointers in a function?

void ChangePointers(int *p_intP1, int *p_intP2); 

int main() {

int i = 100,  j = 500;
int *intP1, *intP2; /* pointers */
intP1 = &i;
intP2 = &j;
printf("%d\n", *intP1); /* prints 100 (i) */
printf("%d\n", *intP2); /* prints 500 (j) */
ChangePointers(intP1, intP2);


printf("%d\n", *intP1); /* still prints  100, would like it swapped by now */
printf("%d\n", *intP2); /* still prints  500 would like it swapped by now */
}/* end main */

void ChangePointers(int *p_intP1, int *p_intP2) {
int *l_intP3; /* local for swap */
l_intP3 = p_intP2;
p_intP2 = p_intP1;
p_intP1= l_intP3;
}
+5  A: 

Change the signature to take a pointer to a pointer.

void ChangePointers(int **p_intP1, int **p_intP2) {
int *l_intP3; /* local for swap */
l_intP3 = *p_intP2;
*p_intP2 = *p_intP1;
*p_intP1= l_intP3;
}

Or, if you want the calling code to look the same, resembling C++ references, use a macro.

void ChangePointersImpl(int **p_intP1, int **p_intP2) {
int *l_intP3; /* local for swap */
l_intP3 = *p_intP2;
*p_intP2 = *p_intP1;
*p_intP1= l_intP3;
}

#define ChangePointers(a,b) ChangePointersImpl(&a, &b)
Shmoopty
+3  A: 

You are changing the pointer's local values inside the function - once it exits, those changes don't persist. If you want to change the values that the pointers point to, you want:

int intP3; /* local for swap */
intP3 = *p_intP2;
*p_intP2 = *p_intP1;
*p_intP1 = intP3;

If you want to change the values of the pointers themselves (e.g. the addresses) you need to pass the pointers to those pointers, not the pointers themselves.

DVK
+12  A: 

You need a pointer to the pointer.

ChangePointers(&intP1, &intP2);

void ChangePointers(int **p_intP1, int **p_intP2) {
        int *l_intP3;
        l_intP3 = *p_intP2;
        *p_intP2 = *p_intP1;
        *p_intP1 = l_intP3;
}
Mak Kolybabi
+1  A: 

If you want to switch the pointers, then you'll have to pass-in the addresses of the pointers rather than the pointer values:

void ChangePointers(int **p_intP1, int **p_intP2) {
int *l_intP3; /* local for swap */
l_intP3 = *p_intP2;
*p_intP2 = *p_intP1;
*p_intP1= l_intP3;
}
Steve Emmerson
+15  A: 

In C, parameters are always passed by values. Although you are changing the values of the pointer variables inside the called function the changes are not reflected back to the calling function. Try doing this:

void ChangePointers(int **p_intP1, int **p_intP2); /*Prototype*/

void ChangePointers(int **p_intP1, int **p_intP2) /*Definition*/
{
    int *l_intP3; /* local for swap */
    l_intP3 = *p_intP2;
    *p_intP2 = *p_intP1;
    *p_intP1= l_intP3;
}

Corresponding call from main() should be:

ChangePointers(&intP1, &intP2);/*Passing in the address of the pointers instead of their values*/
Prasoon Saurav
Ok, great thanks!
Chris_45
+2  A: 

In addition to all these answers, I just wanted to add that when I was having problems understanding pointers in a situation like this I would take it down to something simpler like an int. Say if you were passing an int to a function:

int x = 10;
change_int(x);
printf("%d",x);
void change_int(int y)
{
   y = 20;
}

x will obviously still print 10. To actually change x, you would have to pass a pointer to x so that you can modify x directly.

int x = 10;
change_int(&x);
printf("%d",x);
void change_int(int *y)
{
   *y = 20;
}

now x will print 20. Now just apply this same reasoning as if x was a pointer, then you have to pass a double pointer. Hope this helps.

itscaleb
+1  A: 

Please see here for another similar question as explained. Once you have read this, this will make it easier for you to understand other answers above.

Hope this helps, Best regards, Tom.

tommieb75