Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a C library which declares a function of the following type

void SomeFunction(int a, int(*fp)()) ;

However in the usage within the library , they seem to pass function pointers that take extra parameters
eg. int (*fp)(char * , int)


It seems to be ok in C .

However compiling C++ ( gcc) , this isnt allowed.

Any clues as to which would be the most easiest way to fix this .

I was thinking of casting this but was unsure about which casting type to use - static_cast or reinterpret_cast ?
Posted

In C, when a function is declared without any parameters like int fun(), it means it can take variable number of parameters. If you don't want the function to take any parameters in C, you must explicitly declare it as int fun(void).

In C++, this changed. Functions declared without any parameters are equivalent to being declared with void. Variable arguments would need the ellipsis (...) as parameter.

So for C++ you would need to declare it with ellipsis as mentioned in the following link -
http://msdn.microsoft.com/en-us/library/fxhdxye9(v=vs.80).aspx[^]
 
Share this answer
 
The main reason you can do this is because most flavors of C is not as strongly typed as C++. I.e. you don't need as many type-casts as you would in C++ if you want to 'violate' something.

reinterpret_cast vs static_cast[^]
 
Share this answer
 
v2
typedef int (*FNPARAM)();

int OtherFunc(char* lpc,int i)
{
  return 0;
}

void main()
{
  SomeFunction(1,(FNPARAM)OtherFunc);
}

Thats it, regards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900