Created
July 15, 2019 19:22
-
-
Save garettbass/c0c8b8079b30c7da9d2cc3ef0b846071 to your computer and use it in GitHub Desktop.
A simple in-place swap macro for C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define swap(a, b) do {\ | |
typedef struct { char data[sizeof(a) == sizeof(b) ? sizeof(a) : -1]; } swap;\ | |
swap* const aa = (swap*)&(a);\ | |
swap* const bb = (swap*)&(b);\ | |
swap const cc = *aa; *aa = *bb; *bb = cc;\ | |
} while(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment