473,326 Members | 2,438 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,326 software developers and data experts.

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

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

Similar topics

3
by: JC | last post by:
Hello, I was wondering if there is a way I can check for the TYPE of answer I receive from a user For example I'm writing a program that requires the user to enter a integer (TYPE INT) if...
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
1
by: ChasW | last post by:
Im looking for some technical clarification regarding exactly what the differences, if any, are between the two pieces of code are, aside from the obvious syntactical differences. int x = 5; ...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
1
by: praburam | last post by:
hai everyone private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { if(DropDownList1.SelectedIndex=0) { Panel2.Visible=true; }
2
by: dick | last post by:
are these of the same type? int a; int b; int c; int d; int e;
1
by: Claire | last post by:
"Comparison to integral constant is useless; the constant is outside the range of type 'int'" How can I fix this compiler warning, please, for the following pseudocode? int m_nError; m_nError =...
7
by: junky_fellow | last post by:
Guys, Does the standard allow to an implementation to chose any maximum and minimum value for an object of type int ? For eg. Can an implementation with sizeof(int)=4, chose to have value of...
17
by: Nikhil Bokare | last post by:
#include<stdio.h> int main() { int a; printf("%d %d %d %d",a,*a,**a,&a); } I tried the above code and got the same value for a, *a , **a and &a. Can anyone please tell me the reason behind...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.