Is there a built in swap function in C?

前端 未结 11 2089
北恋
北恋 2020-12-15 16:01

Is there any built in swap function in C which works without using a third variable?

相关标签:
11条回答
  • 2020-12-15 16:15
    #define swap(T, x, y) \
        {                 \
            T tmp = x;    \
            x = y;        \
            y = tmp;      \
        }
    
    int main()
    {
        int a = 10;
        int b = 20;
        printf("a=%d b=%d\n", a, b);
        swap(int, a, b);
        printf("a=%d b=%d\n", a, b);
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-15 16:15

    There is no built-in swap function but you can try this

    a = a ^ b;

    b = a ^ b;

    a = b ^ a;

    0 讨论(0)
  • 2020-12-15 16:17

    There is no such function in standard C.

    (In C++ you have std::swap().)


    Maybe a macro from this question can be useful for you.

    0 讨论(0)
  • 2020-12-15 16:18

    Assuming you want a C solotion, not a C++ one, you could make it a macro, at least using GCC extension to have it generic enough, something like

     #define SWAP(x,y) do {   \ 
       typeof(x) _x = x;      \
       typeof(y) _y = y;      \
       x = _y;                \
       y = _x;                \
     } while(0)
    

    beware of tricks like invocations swap(t[i++],i); to avoid them, use the address operator &. And you'll better use a temporary (for integers, there is a famous and useless trick with exclusive-or).

    PS: I'm using two local variables _x and _y (but I could have used one local variable only) for better readability, and perhaps also to enable more optimizations from the compiler.

    0 讨论(0)
  • 2020-12-15 16:19

    No.
    C++ builtin swap function: swap(first,second);
    Check this: http://www.cplusplus.com/reference/algorithm/swap/

    You can use this to swap two variable value without using third variable:

    a=a^b;
    b=a^b;
    a=b^a;
    

    You can also check this:

    https://stackoverflow.com/questions/756750/swap-the-values-of-two-variables-without-using-third-variable

    How to swap without a third variable?

    0 讨论(0)
  • 2020-12-15 16:19

    I believe I've come up with a type-agnostic function for swapping any two values in standard C, though since I'm fairly new to the language I may have overlooked something. It uses the XOR swap algorithm, and I'm sure it could be optimized more, but it works as long as the two values point to the same number of bytes, specified by the 3rd argument:

    void swapn(void *a, void *b, size_t n) {
        if (a == b) {
            return;
        }
    
        size_t i;
        char *x = (char *)a,
            *y = (char *)b;
    
        for (i = 0; i < n; i++) {
            *x ^= *y;
            *y ^= *x;
            *x ^= *y;
            x++;
            y++;
        }
    }
    

    Example usage:

    // swap two integers
    int x = 5,
        y = 30;
    
    printf("%d\t%d\n", x, y);
    
    swapn(&x, &y, sizeof(int));
    
    printf("%d\t%d\n\n", x, y);
    
    // swap two floats
    float a = 9.23f,
        b = 6.83f;
    
    printf("%.2f\t%.2f\n", a, b);
    
    swapn(&a, &b, sizeof(float));
    
    printf("%.2f\t%.2f\n\n", a, b);
    
    // swap two doubles
    double p = 4.7539,
        q = 0.9841;
    
    printf("%.4f\t%.4f\n", p, q);
    
    swapn(&p, &q, sizeof(double));
    
    printf("%.4f\t%.4f\n\n", p, q);
    
    // swap two chars
    char m = 'M',
        n = 'n';
    
    printf("%c\t%c\n", m, n);
    
    swapn(&m, &n, sizeof(char));
    
    printf("%c\t%c\n\n", m, n);
    
    // swap two strings of equivalent length
    char s[] = "Hello",
        t[] = "World";
    
    printf("%s\t%s\n", s, t);
    
    swapn(s, t, sizeof(s));
    
    printf("%s\t%s\n\n", s, t);
    

    The output is:

    5   30
    30  5
    
    9.23    6.83
    6.83    9.23
    
    4.7539  0.9841
    0.9841  4.7539
    
    M   n
    n   M
    
    Hello   World
    World   Hello
    
    0 讨论(0)
提交回复
热议问题