Last active
December 20, 2015 01:19
-
-
Save x5lcfd/6047814 to your computer and use it in GitHub Desktop.
This is a swap macro in c, and can swap arbitrary types.
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
#include <string.h> | |
#define swap(x, y) do \ | |
{ unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x):-1];\ | |
memcpy(swap_temp, &y, sizeof(x)); \ | |
memcpy(&y, &x, sizeof(x)); \ | |
memcpy(&x, swap_temp, sizeof(x)); \ | |
} while(0) | |
// this snippet from stackoverflow | |
// http://stackoverflow.com/questions/3982348/implement-generic-swap-macro-in-c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment