Skip to content

Instantly share code, notes, and snippets.

@nyux
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyux/234b627f3bd95787770e to your computer and use it in GitHub Desktop.
Save nyux/234b627f3bd95787770e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct {
char *name;
int health, atk, def;
} character_t;
character_t create_character(character_t init)
{
character_t new_char;
new_char.name = strdup(init.name);
new_char.health = init.health;
new_char.atk = init.atk;
new_char.def = init.def;
return new_char;
}
int sum_(int[] arr)
{
int sum = 0;
for (int i = 0; !isnan(arr[i]); i++) sum += arr[i];]
return sum;
}
#define new_character(...) create_character((character_t) {.name = "Blah", .health = 20, .atk = 5, .def = 3, __VA_ARGS__})
#define sum(...) sum_((int[]) {__VA_ARGS__, NAN})
int main(void)
{
character_t alice = new_character(.name = "Alice", .def = 10);
character_t alex = new_character(.name = "Alex", .atk = 10);
character_t blah = new_character();
printf("all their attack power is: %d\n", sum(alice.atk, alex.atk, blah.atk));
/* do stuff, free strings in the end */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment