Skip to content

Instantly share code, notes, and snippets.

@garettbass
Created July 15, 2019 19:22
Show Gist options
  • Save garettbass/c0c8b8079b30c7da9d2cc3ef0b846071 to your computer and use it in GitHub Desktop.
Save garettbass/c0c8b8079b30c7da9d2cc3ef0b846071 to your computer and use it in GitHub Desktop.
A simple in-place swap macro for C
#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