Last active
August 29, 2015 14:01
-
-
Save addisaden/d89c7eaed520a2a8c3a1 to your computer and use it in GitHub Desktop.
THIS FAILS .... Sorry ... How to make closures in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
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
test.c:6:1: warning: ISO C forbids nested functions [-pedantic]