473,387 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pointer to function

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
6 6312
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
16
by: fix | last post by:
Hi all, I am new to C and I just started to program for a homework. I included my code down there. It is a fraction "class". I declared the Fraction struct, tried to create some invalid fraction,...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
10
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.