Skip to content

Instantly share code, notes, and snippets.

@x5lcfd
Last active December 20, 2015 01:19
Show Gist options
  • Save x5lcfd/6047814 to your computer and use it in GitHub Desktop.
Save x5lcfd/6047814 to your computer and use it in GitHub Desktop.
This is a swap macro in c, and can swap arbitrary types.
#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