Skip to content

Instantly share code, notes, and snippets.

@loriopatrick
Created May 26, 2014 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save loriopatrick/2594fd21a09602d60fd5 to your computer and use it in GitHub Desktop.
Save loriopatrick/2594fd21a09602d60fd5 to your computer and use it in GitHub Desktop.
C is awesome!
#include <stdio.h>
#include <stdlib.h>
#define call(VAR, METHOD, ...) (VAR)->METHOD(VAR, __VA_ARGS__)
#define with(TYPE, NAME, VALUE, BODY) {TYPE NAME = VALUE; BODY free(NAME);}
void flyImpl(void* rocket, int x_fore, int y_fore) {
printf("Applying force (%i, %i)\n", x_fore, y_fore);
}
typedef struct {
void (*fly)(void*, int, int);
} Rocket;
int main(int argc, char const *argv[]){
with(char*, test, NULL, {
asprintf(&test, "My name is %s", "Macro Lover");
printf("test said: \"%s\"\n", test);
});
Rocket rocket;
rocket.fly = flyImpl;
call(&rocket, fly, 2, 3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment