Skip to content

Instantly share code, notes, and snippets.

@jballanc
Last active December 17, 2015 06:19
Show Gist options
  • Save jballanc/5564479 to your computer and use it in GitHub Desktop.
Save jballanc/5564479 to your computer and use it in GitHub Desktop.
Fun 'n Games with C: An innocuous seeming pair of parentheses turns an infinite loop into a end-run around the C preprocessor.
#include <stdio.h>
void method(char* first, char* second, char* third)
{
printf("method arguments - first: %s, second: %s, third: %s\n", first, second, third);
return;
}
void wrapped_method(char* first, char* second, char* third);
#define method(first, second, third) wrapped_method((second), (third), (first))
void wrapped_method(char* first, char* second, char* third)
{
printf("wrapped_method arguments - first: %s, second: %s, third: %s\n", first, second, third);
printf("Calling the *real* method...");
(method)(second, third, first); //remove the parens around "method" and the result is very different...
return;
}
int main(int argc, char* argv[])
{
printf("Calling: method(one, two, three)\n");
method("one", "two", "three");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment