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

type void (*FP)(int i=0)

P: n/a
I have problem.I want to create function pointer with typedef so that
I can use it for both functions which takes one integer parameter and
function which take none because of some obvious reasons of code
merging from two different projects.

Something like

type void (*FP)(int i=0);

But above piece of code gives me a error, that I cannot have default
parameters.How can I change my code

Feb 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
hyderabadblues wrote:
I have problem.I want to create function pointer with typedef so that
I can use it for both functions which takes one integer parameter and
function which take none
You can't.
But above piece of code gives me a error, that I cannot have default
parameters.
C doesn't have default parameters. At all.

--
Chris "electric hedgehog" Dollin
"The path to the web becomes deeper and wider" - October Project

Feb 19 '07 #2

P: n/a
hyderabadblues wrote:
I have problem.I want to create function pointer with typedef so that
I can use it for both functions which takes one integer parameter and
function which take none because of some obvious reasons of code
merging from two different projects.
Can't be done without jumping through some cast-related hoops.
Something like

type void (*FP)(int i=0);

But above piece of code gives me a error, that I cannot have default
parameters.How can I change my code
You're thinking of C++, there is no such thing as a default parameter in C.

[OT]
What you're trying to do still wouldn't work in C++.
[/OT]
--
Clark S. Cox III
cl*******@gmail.com
Feb 19 '07 #3

P: n/a
hyderabadblues <si*********@gmail.comwrote:
I have problem.I want to create function pointer with typedef so that
I can use it for both functions which takes one integer parameter and
function which take none because of some obvious reasons of code
merging from two different projects.
Something like
type void (*FP)(int i=0);
But above piece of code gives me a error, that I cannot have default
parameters.How can I change my code
You can't have default parameters for functions - you would need C++
for that. Moreover, having a default argument won't help you also in
C++ if the function to be called doesn't take arguments - you would
only be able to call functions that take single int arguments but
you could leave out that argument when calling the functions (if I
remember that bit about C++ correctly - better ask in comp.lang.c++).

Concerning how to create a type that can store function pointers to
functions returning the same type but with differing arguments here
is a trivial example where it is used that a empty set of arguments
means that the number (and types) of the arguments is unspecified:

#include <stdio.h>

typedef void( * FP )( );

void foo( int x )
{
printf( "%d\n", x );
}

void bar( void )
{
printf( "nothing\n" );
}

int main( void )
{
FP fp = foo;
fp( 2 );
fp = far;
fp( );
return 0;
}

Of course, this will not allow the compiler to check if there are as
many arguments (and if they are of the correct types) as the function
you call needs. That's the price you have to pay. An alternative might
be using a union to tell the compiler explicitely which type the func-
tion has:

#include <stdio.h>

typedef union {
void( * one_int_arg )( int );
void( * no_args )( void );
} FP;

void foo( int x )
{
printf( "%d\n", x );
}

void bar( void )
{
printf( "nothing\n" );
}

int main( void )
{
FP fp[ 2 ];
fp.one_int_arg = foo;
fp.one_int_arg( 2 );
fp.no_args = bar;
fp.no_args( );
return 0;
}
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Feb 19 '07 #4

P: n/a
hyderabadblues wrote:
I have problem.I want to create function pointer with typedef so that
I can use it for both functions which takes one integer parameter and
function which take none because of some obvious reasons of code
merging from two different projects.

Something like

type void (*FP)(int i=0);

But above piece of code gives me a error, that I cannot have default
parameters.How can I change my code
...
In C you can define your typename as

type void (*FP)();

This stands for a pointer to a function that takes _unspecified_ number of
parameters, i.e. it disables argument checks completely. A pointer of this type
can point to any void-returning function (with the exception of variadic functions).

You can use it as follows

void foo(void);
void bar(int);
void baz(double, int);

int main()
{
FP p1 = foo;
FP p2 = bar;
FP p3 = baz;
p1();
p2(5);
p3(5.0, 2);
}

Of course, in this case it is your responsibility to make sure that the calls
you make through such pointers are valid, i.e. the [promoted] types and the
number of arguments match. Otherwise, you'll end up with undefined behavior.

--
Best regards,
Andrey Tarasevich
Feb 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.