5

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;
}
1
  • 5
    In C, everything is passed by value. If you want a function to change something, you need to pass a pointer to it. So, if you want a function to change a pointer, you need to pass a pointer to it. I.e., pass a pointer to pointer. Once you start thinking like that, everything becomes easy (well, not everything, but function-calling at least!) Commented Dec 28, 2009 at 17:30

7 Answers 7

14

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*/
Sign up to request clarification or add additional context in comments.

Comments

13

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;
}

Comments

5

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)

Comments

2

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.

Comments

2

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.

Comments

1

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;
}

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.