function returning a function ptr 
Author Message
 function returning a function ptr

Hi

I am trying to write a function to return a func ptr, but the prototype of
such a function eludes me.

#include <stdio.h>

int (*)(int, int)       func_name(char *test);
int f2(int a, int b);

int
main(int argc, char *argv[]) {
        return 0;

Quote:
}

int (*)(int, int)
        func_name(char *) {
        return f2;

Quote:
}

int
f2(int a, int b) {
        return a;

Quote:
}

compiling with gcc -Wall(version gcc version 2.95.3 20010315 (release)
)(machine 5.8 s28_36 sun4u sparc SUNW,Ultra-5_10) I get
ch.c:3: parse error before `)'
ch.c:11: parse error before `)'

Any ideas, on what can be done?

Regards
Aseem.

--
Using M2, Opera's revolutionary e-mail client: http://www.*-*-*.com/



Fri, 25 Nov 2005 04:15:55 GMT  
 function returning a function ptr

Quote:

> Hi

> I am trying to write a function to return a func ptr, but the prototype of
> such a function eludes me.

> #include <stdio.h>

> int (*)(int, int)       func_name(char *test);

int (*func_name(char *test))(int, int);

Quote:
> int f2(int a, int b);

> int
> main(int argc, char *argv[]) {
>         return 0;
> }

> int (*)(int, int)
>         func_name(char *) {
>         return f2;
> }

int (*func_name(char *test))(int, int)
{
   return f2;
Quote:
}
> int
> f2(int a, int b) {
>         return a;
> }



Fri, 25 Nov 2005 04:25:48 GMT  
 function returning a function ptr
On Sun, 08 Jun 2003 23:25:48 +0300, Nejat AYDIN

Quote:


>> Hi

>> I am trying to write a function to return a func ptr, but the prototype
>> of
>> such a function eludes me.

>> #include <stdio.h>

>> int (*)(int, int)       func_name(char *test);

> int (*func_name(char *test))(int, int);

>> int f2(int a, int b);

>> int
>> main(int argc, char *argv[]) {
>> return 0;
>> }

>> int (*)(int, int)
>> func_name(char *) {
>> return f2;
>> }

> int (*func_name(char *test))(int, int)
> {
> return f2;
> }

>> int
>> f2(int a, int b) {
>> return a;
>> }

This works. I see that I was thinking about it wrong - trying to put the
prototype of the function to be returned before the function name. But that
is not to be done.

Thanks a lot.
Aseem.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/



Fri, 25 Nov 2005 04:32:54 GMT  
 function returning a function ptr

Quote:

> Hi

> I am trying to write a function to return a func ptr, but the prototype of
> such a function eludes me.

If in doubt, keep it simple and build up logically.

#include <stdio.h>

typedef int INT_FUNCTION_INT_INT(int, int);

INT_FUNCTION_INT_INT *func_name(char *test);

int f2(int a, int b);

int
main(int argc, char *argv[]) {

  INT_FUNCTION_INT_INT *p = func_name("hello world");
  int i;
  i = (*p)(6, 42);

        return 0;

Quote:
}

INT_FUNCTION_INT_INT *
func_name(char *) {
        return f2;

Quote:
}

int
f2(int a, int b) {
        return a;

Quote:
}

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Fri, 25 Nov 2005 04:46:15 GMT  
 function returning a function ptr

Quote:

> If in doubt, keep it simple and build up logically.

After building logically, compilation should detect the flaw.
The definition of function func_name requires an identifier
for its parameter. Even though this is a simple demonstration
in which the parameter is not used in the definition, a
parameter name is required per Standard 6.9.1.5.

Quote:

> INT_FUNCTION_INT_INT *
> func_name(char *) {
>         return f2;
> }

--
Al Bowers
Tampa, FL. USA

http://www.geocities.com/abowers822
comp.lang.c


Fri, 25 Nov 2005 09:15:34 GMT  
 function returning a function ptr

Quote:


>> If in doubt, keep it simple and build up logically.

> After building logically, compilation should detect the flaw.

Indeed it should.

Quote:
> The definition of function func_name requires an identifier
> for its parameter.

Indeed it does.

Quote:
> Even though this is a simple demonstration
> in which the parameter is not used in the definition, a
> parameter name is required per Standard 6.9.1.5.

Indeed it is.

Quote:

>> INT_FUNCTION_INT_INT *
>> func_name(char *) {
>>         return f2;
>> }

Indeed.

Oops.

--

R



Fri, 25 Nov 2005 14:15:03 GMT  
 function returning a function ptr

Quote:
> I am trying to write a function to return a func ptr, but the prototype of
> such a function eludes me.

Use a typedef. E.g. :

prototype:

int function (char *a, int b);

typedef :

typedef int function_f (char *a, int b);

Simple isn't? From now, life becomes easy, and you eschew{*filter*}syntaxes.

I let you port this concept to your specific problem.

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
C-library: http://www.*-*-*.com/
FAQ de f.c.l.c : http://www.*-*-*.com/ ~rumeau/fclc/

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
                          -- Jerry Coffin in a.l.c.c++



Fri, 25 Nov 2005 18:26:49 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Function that returns a function ptr

2. Returning ptr to function

3. Returning a function ptr

4. Declaration of function returning ptr to 2-D array

5. ? re function returning ptr to local var

6. typedef the function or the function ptr?

7. Function ptr with void* argument can't be assinged function with anytype* ar

8. Pointer to function returning pointer to function returning...

9. Function returning a function-pointer - how to prototype ?

10. Declaring static function returning pointer to extern function

11. About function that returns function pointer

12. Function returning pointers to functions (repost)

 

 
Powered by phpBB® Forum Software