2

In the below program, I was expecting the printf statement to print

a = b, b = a

But, actually it's printing

a=a, b=b

When I try to print a, b values inside function, it's giving a=b,b=a:::I do not understand why pass be reference is not influencing the actual arguments. What am I missing? Can someone please illustrate?

void swap_pointers(char* a, char* b) {
    char* tmp = a;
    a = b;
    b = tmp;
}

int main() {
    char* a = "a";
    char* b = "b";
    swap_pointers(a, b);
    printf("a = %s, b = %s", a, b);
    return 0;
}
3
  • 1
    See: Swap the pointers of two arrays in a function Commented May 25, 2015 at 4:48
  • 3
    Pointers (like any value) are not passed with Call by Reference semantics in C. Commented May 25, 2015 at 4:58
  • 1
    You should declare every single pointer in this example as const. Commented May 25, 2015 at 6:35

4 Answers 4

4

You're only changing the values of the function arguments within the function. if you want to change the address stored by a pointer that is passed as an argument, you would need to pass a pointer to that pointer.

void swap_pointers(char** a, char** b) {
    char* tmp = *a;
    *a = *b;
    *b = tmp;
}

Then you call it like this

int main() {
    char* a = "a";
    char* b = "b";
    swap_pointers(&a, &b);
    printf("a = %s, b = %s", a, b);
    return 0;
}

What's happening here is, in the function you are setting tmp to the value at a, then the value at a to the value at b, and the value at b to tmp by using the dereference operator "*". And when you pass int the arguments, you are passing the address of a and the address of b with the address operator "&". So in the function you set the value at(*) the address of (&) a variable. And that variable is itself a pointer.

Sign up to request clarification or add additional context in comments.

Comments

0

The pointer ("reference" of "pass by reference") is passed by value. If you want to change the reference, you need to pass the reference by reference: (char **a, char **b), and have pointer-to-pointer-to-char variables that you would be able to switch. Your function would work for swapping contents of those pointers; but it can't swap pointers themselves.

Comments

0
#include <stdio.h>
void swap_pointers(char *a, char *b) {
    char tmp = *a;
    *a = *b;
    *b = tmp;
}



int main() {
    char a = 'a';
    char b = 'b';
    swap_pointers(&a,&b);
    printf("a = %c, b = %c", a, b);
    return 0;
}

2 Comments

first you were changing the address not the value assigned to it
Try to use proper formatting in code, as well as some explanations. Only code block is not recommended.
0

Well, this is a very simple and a basic problem, related to function calls.

When you do this swap_pointers(a, b);,

it actually passes the copies of a and b to the function;

which does get swapped inside the function.

But, then you do nothing inside the function and the control is returned back to the main.

As a result, there has not been made any change inside the main.

This is what you are experiencing. The solution to your problem is to use call by reference.

As, it has already been suggested by user2864740 in a comment and explained by Dave in his answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.