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

H

hyderabadblues

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
 
C

Chris Dollin

hyderabadblues said:
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.
 
C

Clark S. Cox III

hyderabadblues said:
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]
 
J

Jens Thoms Toerring

hyderabadblues said:
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
 
A

Andrey Tarasevich

hyderabadblues said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top