tags:

views:

262

answers:

5

i know how to swap 2 variables in c++ . ie you use std::swap(a,b).

question:

does the c standard library have a similar function to c++ std::swap() or do i have to define it myself

+8  A: 

Yes you need to define it yourself.

  1. C doesn't have templates.
  2. If such function does exist it would look like void swap(void* a, void* b, size_t length), but unlike std::swap, it's not type-safe.
  3. And there's no hint such function could be inlined, which is important if swapping is frequent (in C99 there's inline keyword).
  4. We could also define a macro like

    #define SWAP(a,b,type) {type ttttttttt=a;a=b;b=ttttttttt;}
    

    but it shadows the ttttttttt variable, and you need to repeat the type of a. (In gcc there's typeof(a) to solve this, but you still cannot SWAP(ttttttttt,anything_else);.)

  5. And writing a swap in place isn't that difficult either — it's just 3 simple lines of code!

KennyTM
Why the downvote? It answers the op question...
Tom
+6  A: 

There is no equivalent in C - in fact there can't be, as C doesn't have template functions. You will have to write separate functions for all the types you want to swap.

anon
so c has no standard library algorithms? swapping is a commonly used feature
Dr Deo
@Dr: C doesn't have a default data structure library.
KennyTM
@Dr Deo C doesn't have lots of things that C++ does - that's kind of the point.
anon
Outside of writing your own sorting algorithm, what would it be needed for? And C already has qsort().
Ken
@Ken actually, in C++ swap() is used in all sorts of "creative" ways! Most of which would not admittedly apply to C code.
anon
+6  A: 

You can do something similar with a macro if you don't mind using a gcc extension to the C language, typeof:

#include <stdio.h>

#define SWAP(a, b) do { typeof(a) temp = a; a = b; b = temp; } while (0)

int main(void)
{
    int a = 4, b = 5;
    float x = 4.0f, y = 5.0f;
    char *p1 = "Hello";
    char *p2 = "World";

    SWAP(a, b); // swap two ints, a and b
    SWAP(x, y); // swap two floats, x and y
    SWAP(p1, p2); // swap two char * pointers, p1 and p2

    printf("a = %d, b = %d\n", a, b);
    printf("x = %g, y = %g\n", x, y);
    printf("p1 = %s, p2 = %s\n", p1, p2);

    return 0;
}
Paul R
Why the down-vote ? And no comment ?
Paul R
The problem with this implementation (portability aside) is that it will blindly swap anything, without regard to semantics (for example if dynamic memory allocation is involved). The C++ swap will call constructors and destructors appropriately to handle this. Downvote wasn't me, BTW.
anon
@Neil: true, but the OP was specifically asking for a `C` implementation. It's far from a perfect solution, but it's a reasonable solution for a significant subset of C applications (IMNVHO).
Paul R
+1  A: 

Another macro not already mentioned here: You don't need to give the type if you give the temporary variable instead. Additionally the comma operator is useful here to avoid the do-while(0) trick. But usually I don't care and simply write the three commands. On the other hand a temporary macro is useful if a and b are more complex.

#define SWAP(a,b,t) ((t)=(a), (a)=(b), (b)=(t))

void mix_the_array (....)
{
    int tmp;
    .....
    SWAP(pointer->array[counter+17], pointer->array[counter+20], tmp);
    .....
}

#undef SWAP
Secure
A: 

Check your compiler documentation. The compiler may have a swapb function for swapping bytes and my provide other similar functions.

Worst case, waste a day and write some generic swap functions. It won't consume a significant amount of your project's schedule.

Thomas Matthews