×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Function pointer to 'extern C'

Function pointer to 'extern C'

Function pointer to 'extern C'

(OP)
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



RE: Function pointer to 'extern C'

This site should explain all the ins and outs of function pointers.
http://www.newty.de/fpt/index.html

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

RE: Function pointer to 'extern C'

(OP)
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

RE: Function pointer to 'extern C'

Use good old brutal force method (if you know it's OK in func1;):

CODE

typedef int(*PFI)();
func1(whatElse,(PFI)func2);

RE: Function pointer to 'extern C'

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.

RE: Function pointer to 'extern C'

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++.

RE: Function pointer to 'extern 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.

RE: Function pointer to 'extern C'

(OP)
it works !!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close