Skip to content

Instantly share code, notes, and snippets.

@letoh
Forked from lancetw/fizzBuzz.c
Last active February 14, 2017 06:08
Show Gist options
  • Save letoh/4da8eb6d1fb3f89c87e6e71b3ff7ad9c to your computer and use it in GitHub Desktop.
Save letoh/4da8eb6d1fb3f89c87e6e71b3ff7ad9c to your computer and use it in GitHub Desktop.
char** fizzBuzz(int n, int* returnSize) {
struct _ {
char *rets[n + 1];
char data[0];
} * ret = calloc(sizeof(struct _) + n * 9 + 1, sizeof(char));
*returnSize = n;
char * sp = ret->data;
for (int i = 1; i <= n; ++i, sp += 9) {
ret->rets[i - 1] = sp;
(i % 5 && i % 3)
? sprintf(sp, "%d", i)
: sprintf(sp, "%s%s", (i % 3)
? ""
: "Fizz",
(i % 5)
? ""
: "Buzz");
}
ret->rets[n] = sp;
return ret->rets;
}
int main(int argc, char *argv[])
{
int size;
char **ret = fizzBuzz(15, &size);
for (char **p = ret; **p; ++p)
printf("%s\n", *p);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment