<p>As the title already states, I'm trying to declare a nested function and return a pointer to that function. I want this function 'not' to return a new function pointer which will return the negation of whatever the original function was.</p>

<p>Here is what I have:</p>

<pre><code>someType not( someType original ) {
    int isNot( ListEntry* entry ) {
        return !original( entry );
    }

    someType resultFunc = calloc( 1024, 1 );
    memcpy( resultFunc, &amp;isNot, 1024 );

    return resultFunc;
}
</code></pre>

<p>someType is defined as:</p>

<pre><code>typedef int(*someType)(ListEntry* entry)
</code></pre>

[{'answer_id': 40537, 'author': 'Steve Willard', 'author_id': 402, 'author_profile': 'https://Stackoverflow.com/users/402', 'pm_score': 0, 'selected': False, 'text': "<p>I'm using GCC.</p>\n\n<p>You can turn on nested functions by using the flag:</p>\n\n<pre><code>-fnested-functions\n</code></pre>\n\n<p>when you compile.</p>\n"}, {'answer_id': 40596, 'author': 'Jim Buck', 'author_id': 2666, 'author_profile': 'https://Stackoverflow.com/users/2666', 'pm_score': 0, 'selected': False, 'text': '<p>I also never heard of nested functions in C, but if gcc supports it, this is not going to work the way you expect. You are just simply copying the machine instructions of isNot, and that won\'t include the actual value of "original" at the time "not" is being called.</p>\n\n<p>You should use a C++ class to implement a <a href="http://en.wikipedia.org/wiki/Function_object" rel="nofollow noreferrer">function object</a> that stores a pointer that you can initialize with the value of "original" and return an instance of this class from "not".</p>\n'}, {'answer_id': 40757, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>Steve, you have a completely wrong mental model of what is a C function. </p>\n\n<pre><code>someType resultFunc = calloc( 1024, 1 );\nmemcpy( resultFunc, &amp;isNot, 1024 );\n</code></pre>\n\n<p>From your code fragment, I can surmise that you think that you can copy function\'s compiled code into a block of memory, and then reuse it. This kind of thing smells of Lisp, except even in lisp you don\'t do it that way.</p>\n\n<p>In fact, when you say "&amp;isNot", you get a pointer to function. Copying the memory that pointer points at is counterproductive - the memory was initialized when you loaded your executable into memory, and it\'s not changing. In any case, writing someFunc() would cause a core dump, as the heap memory behing someFunc cannot be executed - this protects you from all sorts of viruses.</p>\n\n<p>You seem to expect an implementation of closures in C. That implementation is simply not there. Unlike Lisp or Perl or Ruby, C cannot preserve elements of a stack frame once you exited that frame. Even is nested functions are permitted in some compilers, I am sure that you cannot refer to non-global variables from inside those functions. The closes thing to closures is indeed C++ object that stores the state and implements operator(), but it\'s a completely different approach, and you\'d still have to do things manually.</p>\n\n<p>Update: <a href="http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Nested-Functions.html#Nested-Functions" rel="noreferrer">here</a> is the relevant portion of GCC documentation. Look for "But this technique works only so long as the containing function (hack, in this example) does not exit."</p>\n'}, {'answer_id': 525951, 'author': 'Logan Capaldo', 'author_id': 61289, 'author_profile': 'https://Stackoverflow.com/users/61289', 'pm_score': 1, 'selected': False, 'text': "<p>You're not going to be able to do this in the fashion you want. You have a couple of alternative options.</p>\n\n<p>You can use macros:</p>\n\n<pre><code>#define FN_NOT(F) !F\n#define notSomeFunc FN_NOT(someFunc)\n...\nx = notSomeFunc(entry);\n</code></pre>\n\n<p>But I suspect you wanted to be able to pass the negated function around to other functions that take function pointers, so that won't work.</p>\n\n<p>You can change your interfaces to accept some extra information, eg</p>\n\n<pre><code>struct closure {\n  void *env;\n  int (*f)(struct closure* extra, ListEntry*);\n};\n\nstatic int isNot(struct closure* extra, ListEntry *entry) {\n  someType original = extra-&gt;env;\n  return !original(entry);\n}\n\nstruct closure not(someType original) {\n   closure rv;\n   rv.env = original;\n   rv.f = &amp;isNot;\n   return rv;\n}\n</code></pre>\n\n<p>And then use it like:</p>\n\n<pre><code>struct closure inverse_fn;\ninverse_fn = not( &amp;fn );\nif( inverse_fn.f(&amp;inverse_fn, entry) ) {\n    ...\n}\n</code></pre>\n\n<p>There are other things you can try, like JITing functions at runtime, but those sorts of techniques are going to be platform and architecture dependent. This solution is awkward, but pure C and portable.</p>\n"}]
['https://Stackoverflow.com/questions/40511', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/402/']