Hello,
I am getting a compilation error for this:
What I am trying to declare is a function that returns a pointer to a function taking an int argument and returning an int. Can anybody tell me where the problem is? Thanks.Code:int(*)(int)fun();
This is a discussion on Function returning pointer to function within the C++ Programming forums, part of the General Programming Boards category; Hello, I am getting a compilation error for this: Code: int(*)(int)fun(); What I am trying to declare is a function ...
Hello,
I am getting a compilation error for this:
What I am trying to declare is a function that returns a pointer to a function taking an int argument and returning an int. Can anybody tell me where the problem is? Thanks.Code:int(*)(int)fun();
When I get confused, I just "cheat" with a typedef:Originally Posted by DL1
It makes the code more readable, anyway.Code:typedef int (*Function)(int); Function fun();
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
It is easiest if you use a typedef:
Code:typedef int (*fn)(int); // typedef fn as a function that takes an int and returns an int fn fun(); // Declare fun() which returns a function that takes an int and returns an int
bit∙hub [bit-huhb] n. A source and destination for information.
Thanks. That's what I ended up doing. I am still puzzled as to why my initial declaration didn't work though. I can't see any difference between it and the typedef version, other than that the latter introduces a new name.
So far as I can tell, the real version is this:
What you originally posted is ... well, I'm pretty sure it's not even a little bit syntactically valid.Code:int (*(fun(void))) (int);
Nicely put. I see now where I went wrong, I think.
And if the function took an argument of the same pointer type, it would presumably be declared as follows:
Code:int (*(fun(int(*)(int)))) (int);
Last edited by DL1; 08-07-2009 at 10:52 AM.
That's correct.
bit∙hub [bit-huhb] n. A source and destination for information.