Skip to content

Instantly share code, notes, and snippets.

@addisaden
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save addisaden/d89c7eaed520a2a8c3a1 to your computer and use it in GitHub Desktop.
Save addisaden/d89c7eaed520a2a8c3a1 to your computer and use it in GitHub Desktop.
THIS FAILS .... Sorry ... How to make closures in C
#include <stdio.h>
void (*greeter(char greeting[]))(char[])
{
int intern_position = 1;
void result(char name[])
{
if(intern_position) return;
printf("%s, %s!\n", greeting, name);
}
result(""); // without this function call
// the function wouldnt exist
// and throw an segmentation
// fault.
intern_position = 0;
return result;
}
int main()
{
void (*g)(char[]);
g = greeter("Hallo");
g("Welt");
// after some notes and comments another test
g("hackernews"); // THIS FAILS - segmentation error.
return 0;
}
@jjg-aframe
Copy link

test.c:6:1: warning: ISO C forbids nested functions [-pedantic]

@addisaden
Copy link
Author

Thank you for your Feedback :)

after testing i found out, that this wouldnt work! See on line 31.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment