By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Pointer to function

P: n/a
I wonder if the following is possible:

I would like to have a function with, say, three arguments. The
first two would be int, whereas the third one would be a pointer to a
generic function. By this I mean a function that can take any number of
arguments, of any type, and that can return anything, including void.

Is this allowed under ANSI C?
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Jim Ford <jf*****@excite.com> wrote in
news:pa****************************@excite.com:
I wonder if the following is possible:

I would like to have a function with, say, three arguments. The
first two would be int, whereas the third one would be a pointer to a
generic function. By this I mean a function that can take any number of
arguments, of any type, and that can return anything, including void.

Is this allowed under ANSI C?


Sure.

int func(int a, int b, void (*pFunc)(char *pFmt, ...));

int main(void)
{
func(1, 2, printf);

return 0;
}

--
- Mark ->
--
Nov 13 '05 #2

P: n/a
In message <pa****************************@excite.com>
Jim Ford <jf*****@excite.com> wrote:
I wonder if the following is possible:

I would like to have a function with, say, three arguments. The
first two would be int, whereas the third one would be a pointer to a
generic function. By this I mean a function that can take any number of
arguments, of any type, and that can return anything, including void.

Is this allowed under ANSI C?


Not straightforwardly. You can leave the arguments unspecified, but that's
deprecated, and precludes any arguments actually being "narrow" types (ie
char, short, float). You have to specify a specific return type.

However, the standard says that you can store any function pointer value
in a function pointer of another type without loss - basically the same
guarantee as given for void * storing object pointers. So you could fudge it
thus, quite legally:

typedef void (*GenericFnPtr)(void); // any function ptr type would do

void jims_func(int a, int b, GenericFnPtr fn);

int myfunc(int a);

jims_func(1, 2, (GenericFnPtr) myfunc);

void jims_func(int a, int b, GenericFnPtr fn)
{
int n;
switch (a)
{
case 1: n = (((int (*)(int))p)(b);
break;
...
}
...
}

You will have to do all the icky casting, but maybe it could be tidied with
some more typedefs and/or macros. And obviously you'll have to know what
the actual type of the stored function actually is, so you can cast it back
to the correct type before use.

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #3

P: n/a
In <Xn********************************@130.133.1.4> "Mark A. Odell" <no****@embeddedfw.com> writes:
Jim Ford <jf*****@excite.com> wrote in
news:pa****************************@excite.com:
I wonder if the following is possible:

I would like to have a function with, say, three arguments. The
first two would be int, whereas the third one would be a pointer to a
generic function. By this I mean a function that can take any number of
arguments, of any type, and that can return anything, including void.

Is this allowed under ANSI C?


Sure.

int func(int a, int b, void (*pFunc)(char *pFmt, ...));


Are you sure you have declared pFunc as a pointer to a function that can
take *any* number of arguments, of any type and return *anything*?

The only bit that's actually impossible is returning anything. A function
declaration must specify a return type and there is no way around. The
workaround is to make the candidate functions return a union containing
all the types of interest.

The any number of arguments of any type can be easily obtained by not
declaring an argument list at all. There are a couple of limitations,
however:

1. The function call must be compatible with the actual definition of the
called function. The compiler cannot check this, but any mismatch
results in undefined behaviour.

2. The function parameters must not have types that are subject to the
default argument promotions: because the function pointer declaration
doesn't include a prototype, the default argument promotions cannot
be avoided.

With these in mind, the prototype of the OP's function is:

union omnia;
int func(int a, int b, union omnia (*pFunc)());

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4

P: n/a
Da*****@cern.ch (Dan Pop) wrote in news:bp*********@sunnews.cern.ch:
Is this allowed under ANSI C?


Sure.

int func(int a, int b, void (*pFunc)(char *pFmt, ...));


Are you sure you have declared pFunc as a pointer to a function that can
take *any* number of arguments, of any type and return *anything*?


No, I guess not. I can't return anything just one type.

--
- Mark ->
--
Nov 13 '05 #5

P: n/a
In <Xn********************************@130.133.1.4> "Mark A. Odell" <no****@embeddedfw.com> writes:
Da*****@cern.ch (Dan Pop) wrote in news:bp*********@sunnews.cern.ch:
Is this allowed under ANSI C?

Sure.

int func(int a, int b, void (*pFunc)(char *pFmt, ...));


Are you sure you have declared pFunc as a pointer to a function that can
take *any* number of arguments, of any type and return *anything*?


No, I guess not. I can't return anything just one type.


And you can't call it with no arguments or with a single argument of type
int, and so on.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

P: n/a
Da*****@cern.ch (Dan Pop) wrote in news:bp**********@sunnews.cern.ch:
> Is this allowed under ANSI C?

Sure.

int func(int a, int b, void (*pFunc)(char *pFmt, ...));

Are you sure you have declared pFunc as a pointer to a function that
can take *any* number of arguments, of any type and return *anything*?


No, I guess not. I can't return anything just one type.


And you can't call it with no arguments or with a single argument of
type int, and so on.


This is turning out horribly, I've missed the mark on all counts.

--
- Mark ->
--
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.