Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function pointer to 'extern C'

Status
Not open for further replies.

fheyn

Programmer
Mar 22, 2001
198
DE
Hi good morning (well, at least here in germany it's morning)

now it's more than a day I've been trying to solve the following problem :

from c++-application I call extern function func1

func1 has as argument pointer to func2.

i cannot modify func1 and func2

however I'm unable to set the pointer to func2 from c++.

func1 looks like this : func1(char*, int(*)(());

func2 looks like this : func2(char**, char**);

compiler does not accept func1(MyChar, func2)

Any ideas how to solve this or with this 'C' and 'c++'

won't work together.

thanks in advance

franz



 
hi, great site but no solution to my problem

I will describe it differntly:

func1(char*, int(*)()) is hidden somewhere in an old c-library
the 2nd argument of func1 is a pointer to
func2(char**, char**)
both functions are declared in the c++-app using 'extern C'
I must, however, declare func1 as it is declared in the c-library
calling func1 from my app like func1(somestring, func1) the compiler will tell me that the arguments don't match because I am sending int(*)(char**,char**)

hope this explains a bit better

 
Use good old brutal force method (if you know it's OK in func1;):
Code:
typedef int(*PFI)();
func1(whatElse,(PFI)func2);
 
This works for me with gcc
Code:
// foo.c
#include <stdio.h>

/* C functions as described */
int foo ( char **a, char **b ) {
    printf("%s %s\n", a[0], b[0] );
    return 42;
}

int bar ( char *a, int (*fn)(char**, char**) ) {
    char *arr[] = { "world" };
    return fn( &a, arr );
}

// foo.cpp
#include <fstream>
#include <iostream>
using namespace std;

/* extern declarations for the C functions, and a */
/* handy typedef for the function pointer. */
extern "C" {
    int foo ( char **a, char **b );
    int bar ( char *a, int (*fn)(char**, char**) );
    typedef int (*fooptr)(char**,char**);
}

/* an equivalent C function, which happens to be compiled */
/* with C++ */
extern "C" int another ( char *a, fooptr fn ) {
    char *arr[] = { "earth" };
    return fn( &a, arr );
}

int main ( ) {
    fooptr  aFn = foo;
    int     result;

    result = bar( "hello", aFn );
    cout << "Result=" << result << endl;

    result = another( "goodbye", aFn );
    cout << "Result=" << result << endl;
    return 0;
}

// results
$ gcc -c -W -Wall -ansi -pedantic foo.c
$ g++ -W -Wall -ansi -pedantic foo.cpp foo.o
$ ./a.exe
hello world
Result=42
goodbye earth
Result=42

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Salem's code should work on MS as well.

At a guess, you haven't got the extern "C" so it is assuming that you have a C++ function and is adding extra bits which tell the linker that it is a C++ function instead of a C function.

If you are calling C from C and have the /Tp parameter set in the compile line, it will treat C programs as C++.
 
C allowed to make simple function pointers (ie without specifying the exact amount and type of parameters), but then you had to make sure you call it correctly through a pointer, so you dont mess the stack up.

C++ does a better type-checking so you're not allowed to do the old way anymore, unless you cast it yourself, like ArkM suggested.

Salem, I guess your example is not quite with the problem. Changing the declaration of bar function to the following would make it (ie skip the parameter declaration of function you point at).

Code:
int bar (char *a, int (*fn)())


------------------
When you do it, do it right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top