Skip to content

Instantly share code, notes, and snippets.

@tov
Created September 3, 2019 17:36
Show Gist options
  • Save tov/f620782f6c31378bac3ea18cd83f0f12 to your computer and use it in GitHub Desktop.
Save tov/f620782f6c31378bac3ea18cd83f0f12 to your computer and use it in GitHub Desktop.
Isorecursive function type in C
#include <stdio.h>
typedef struct rec rec_t;
typedef rec_t(*ptr_t)();
struct rec
{
ptr_t ptr;
};
rec_t odd();
rec_t even()
{
puts("even");
return (rec_t) { odd };
}
rec_t odd()
{
puts("odd");
return (rec_t) { even };
}
int main()
{
rec_t my_rec = { .ptr = even };
for (int i = 0; i < 16; ++i)
my_rec = my_rec.ptr();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment