A function in C that returns a function that adds x to its argument
In a thread on JoS, Quickie asked,
"Write a function in C that returns a function that adds x to its argument."
http://discuss.fogcreek.com/joelonso...&ixPost=146457
Here's my go at it. It compiles, links, but prints junk. So it basically doesn't do what it was supposed to do. Comments welcome.
Code:
/*Write a function in C that returns a
function that adds x to its argument
x in this case is 2
*/
#include <stdio.h>
int Add2(int);
typedef int(*fnptr)(int);
int (*fn(void))(int);
int main(void)
{
int (*fnptr)(int);
fnptr=fn;
printf("%d", (*fnptr)(3));
}
int Add2(int a)
{
return a+2;
}
fnptr fn(void)
{
return &Add2;
}