Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6905423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:10:54+00:00 2026-05-27T08:10:54+00:00

I have been struggling to understand the different behaviour when swapping pointers in C.

  • 0

I have been struggling to understand the different behaviour when swapping pointers in C.
If I want to swap two int pointers, then I can do

void intSwap (int *pa, int *pb){
    int temp = *pa;
    *pa = *pb;
    *pb = temp;
}

However, if I want to swap two char pointers I need to do something like

void charSwap(char** a, char** b){
    char *temp = *a;
    *a = *b;
    *b = temp;
}

because if I do

void charSwap(char* a, char* b){
    char temp = *a;
    *a = *b;
    *b = temp;
}

the compiler complains about the expression *a = *b as it cannot change the values.
If I want to swap two strings (i.e. char* s1= "Hello"; char* s2="Bye"; )how would one do it?

Could you please give me a little bit of help? I would like to really learn how it works so I will not need to experience trial and error all the time until I get the right answer.
I hope it’s useful for many other people.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:10:54+00:00Added an answer on May 27, 2026 at 8:10 am

    The first thing you need to understand is that when you pass something to a function, that something is copied to the function’s arguments.

    Suppose you have the following:

    void swap1(int a, int b) {
        int temp = a;
        a = b;
        b = temp;
        assert(a == 17);
        assert(b == 42);
        // they're swapped!
    }
    
    int x = 42;
    int y = 17;
    swap1(x, y);
    assert(x == 42);
    assert(y == 17);
    // no, they're not swapped!
    

    The original variables will not be swapped, because their values are copied into the function’s arguments. The function then proceeds to swap the values of those arguments, and then returns. The original values are not changed, because the function only swaps its own private copies.

    Now how do we work around this? The function needs a way to refer to the original variables, not copies of their values. How can we refer to other variables in C? Using pointers.

    If we pass pointers to our variables into the function, the function can swap the values in our variables, instead of its own argument copies.

    void swap2(int* a, int* b) {
        int temp = *a;
        *a = *b;
        *b = temp;
        assert(*a == 17);
        assert(*b == 42);
        // they're swapped!
    }
    
    int x = 42;
    int y = 17;
    swap2(&x, &y); // give the function pointers to our variables
    assert(x == 17);
    assert(y == 42);
    // yes, they're swapped!
    

    Notice how inside the function we’re not assigning to the pointers, but assigning to what they point to. And the pointers point to our variables x and y. The function is changing directly the values stored in our variables through the pointers we give it. And that’s exactly what we needed.

    Now what happens if we have two pointer variables and want to swap the pointers themselves (as opposed to the values they point to)? If we pass pointers, the pointers will simply be copied (not the values they point to) to the arguments.

    void swap3(int* a, int* b) {
        int* temp = a;
        a = b;
        b = temp;
        assert(*a == 17);
        assert(*b == 42);
        // they're swapped!
    }
    void swap4(int* a, int* b) {
        int temp = *a;
        *a = *b;
        *b = temp;
        assert(*a == 17);
        assert(*b == 42);
        // they're swapped!
    }
    
    int x = 42;
    int y = 17;
    int* xp = &x;
    int* yp = &y;
    swap3(xp, yp);
    assert(xp == &x);
    assert(yp == &y);
    assert(x == 42);
    assert(y == 17);
    // Didn't swap anything!
    swap4(xp, yp);
    assert(xp == &x);
    assert(yp == &y);
    assert(x == 17);
    assert(y == 42);
    // Swapped the stored values instead!
    

    The function swap3 only swaps its own private copies of our pointers that it gets in its arguments. It’s the same issue we had with swap1. And swap4 is changing the values our variables point to, not the pointers! We’re giving the function a means to refer to the variables x and y but we want them to refer to xp and yp.

    How do we do that? We pass it their addresses!

    void swap5(int** a, int** b) {
        int* temp = *a;
        *a = *b;
        *b = temp;
        assert(**a == 17);
        assert(**b == 42);
        // they're swapped!
    }
    
    
    int x = 42;
    int y = 17;
    int* xp = &x;
    int* yp = &y;
    swap5(&xp, &yp);
    assert(xp == &y);
    assert(yp == &x);
    assert(x == 42);
    assert(y == 17);
    // swapped only the pointers variables
    

    This way it swaps our pointer variables (notice how xp now points to y) but not the values they point to. We gave it a way to refer to our pointer variables, so it can change them!

    By now it should be easy to understand how to swap two strings in the form of char* variables. The swap function needs to receive pointers to char*.

    void swapStrings(char** a, char** b){
        char *temp = *a;
        *a = *b;
        *b = temp;
        assert(strcmp(*a, "world") == 0);
        assert(strcmp(*b, "Hello") == 0);
    }
    
    char* x = "Hello";
    char* y = "world";
    swapStrings(&x, &y);
    assert(strcmp(x, "world") == 0);
    assert(strcmp(y, "Hello") == 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have for the last several years been struggling to understand why the Internet
been struggling with an issue now for a day or two. I have an
I'm unfamiliar with Javascript and have been struggling to understand how I'd go about
So, for the past few days I have been struggling to understand how to
I have been struggling with this for a while and just can't work it
I have been struggling to get this right! Can anyone help me to convert
I have been struggling with this issue for 3 hours. I know I can
Bits and bitmask are something I have been struggling to understand for a while,
I have been struggling with this one for days and can't seem to figure
I have been struggling to understand the difference between Storyboards and coding in Objective-C.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.