function pointer to extern?

Discussion in 'C++' started by Travis, Jun 4, 2007.

  1. Travis

    Travis Guest

    I'm relatively new to externs and function pointers but have inherited
    the task of modifying some existing code. I have something like this.

    extern void myExternFunc (MenuItem *item, void * param)
    void (*fptr)(MenuItem *item, void * param) = myExternFunc;

    This doesn't compile. I don't understand why. Obviously something like
    this:

    extern void myFunc (MenuItem *item, void * param) { }
    void (*fptr)(MenuItem *item, void * param) = myFunc

    compiles fine. The only thing I can think to check is that you can
    creat function pointer to externs.

    Thanks.
    Travis, Jun 4, 2007
    #1
    1. Advertising

  2. See my insetrs bellow:

    Travis wrote:
    > I'm relatively new to externs and function pointers but have inherited
    > the task of modifying some existing code. I have something like this.
    >
    > extern void myExternFunc (MenuItem *item, void * param)


    semicolon ";" is missing after above line.

    > void (*fptr)(MenuItem *item, void * param) = myExternFunc;
    >
    > This doesn't compile. I don't understand why. Obviously something like
    > this:
    >
    > extern void myFunc (MenuItem *item, void * param) { }


    when doing extern, you should not declare the body.

    > void (*fptr)(MenuItem *item, void * param) = myFunc
    >
    > compiles fine. The only thing I can think to check is that you can
    > creat function pointer to externs.
    >
    > Thanks.
    >
    Haro Panosyan, Jun 4, 2007
    #2
    1. Advertising

  3. Travis wrote:
    > I'm relatively new to externs and function pointers but have inherited
    > the task of modifying some existing code. I have something like this.
    >
    > extern void myExternFunc (MenuItem *item, void * param)


    A semicolon is missing at the end of the previous line.

    > void (*fptr)(MenuItem *item, void * param) = myExternFunc;
    >
    > This doesn't compile. I don't understand why. Obviously something like
    > this:
    >
    > extern void myFunc (MenuItem *item, void * param) { }
    > void (*fptr)(MenuItem *item, void * param) = myFunc
    >
    > compiles fine. The only thing I can think to check is that you can
    > creat function pointer to externs.


    All functions are 'extern' by default, IIRC, unless they are 'static'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 4, 2007
    #3
  4. Travis

    Rolf Magnus Guest

    Travis wrote:

    > I'm relatively new to externs and function pointers but have inherited
    > the task of modifying some existing code. I have something like this.
    >
    > extern void myExternFunc (MenuItem *item, void * param)
    > void (*fptr)(MenuItem *item, void * param) = myExternFunc;
    >
    > This doesn't compile.


    What does "doesn't compile" mean? You should post a minimal, but complete
    program together with the compiler error message.
    Rolf Magnus, Jun 4, 2007
    #4
  5. Travis

    Mark P Guest

    Travis wrote:
    > I'm relatively new to externs and function pointers but have inherited
    > the task of modifying some existing code. I have something like this.
    >
    > extern void myExternFunc (MenuItem *item, void * param)
    > void (*fptr)(MenuItem *item, void * param) = myExternFunc;
    >
    > This doesn't compile. I don't understand why. Obviously something like
    > this:


    As Victor already mentioned, the keyword "extern" is unneeded here.
    Beyond that, all you need is a ';' after the declaration of
    myExternFunc. Or you can provide a definition as below (and if you
    don't, you'll have to define it /somewhere/).

    Mark

    >
    > extern void myFunc (MenuItem *item, void * param) { }
    > void (*fptr)(MenuItem *item, void * param) = myFunc
    >
    > compiles fine. The only thing I can think to check is that you can
    > creat function pointer to externs.
    >
    > Thanks.
    >
    Mark P, Jun 4, 2007
    #5
  6. Travis

    Travis Guest

    Forgive me. The ';' is there, I just didn't select everything before
    doing a copy/paste.

    Second, the extern is there because the function is actually
    declared / implemented somewhere else. The extern code is the part I
    inherited. The line below it with the function pointer is what I'm
    adding.

    I have made some progress and I think a better question might be
    asking if the following is allowed.

    - a struct containing a few char * and a function pointer
    - a linked list w/ the struct as nodes
    - the head of the linked list passed as a void pointer to elsewhere in
    the app

    Something I'm doing is freezing up my machine entirely. I have a
    feeling it's dereferencing related.
    Travis, Jun 4, 2007
    #6
  7. Travis

    Mark P Guest

    Travis wrote:
    > Forgive me. The ';' is there, I just didn't select everything before
    > doing a copy/paste.


    Please quote context when replying. Like I've done here.

    >
    > Second, the extern is there because the function is actually
    > declared / implemented somewhere else. The extern code is the part I
    > inherited. The line below it with the function pointer is what I'm
    > adding.


    You're missing the point. The "extern" is not required. Without
    additional specifiers, functions have external linkage by default. The
    "extern" is entirely superfluous.

    >
    > I have made some progress and I think a better question might be
    > asking if the following is allowed.
    >
    > - a struct containing a few char * and a function pointer
    > - a linked list w/ the struct as nodes
    > - the head of the linked list passed as a void pointer to elsewhere in
    > the app


    This is a very vague question which makes it hard to give you a useful
    answer. From what little you've specified, it could be fine, but
    without seeing what you're doing, no one really knows.

    >
    > Something I'm doing is freezing up my machine entirely. I have a
    > feeling it's dereferencing related.
    >
    Mark P, Jun 5, 2007
    #7
  8. Travis

    James Kanze Guest

    On Jun 4, 9:26 pm, Haro Panosyan <> wrote:

    [...]
    > > extern void myFunc (MenuItem *item, void * param) { }


    > when doing extern, you should not declare the body.


    That's a frequent convention, but is not based on anything in
    the language. In a function declaration, "extern" has no effect
    as to whether the declaration is a definition or not---all it
    says is that the linkage is external, which is the default for
    functions anyway. As a matter of style, I do put the extern in
    front of the declaration, in the header, and I don't put it in
    front of the definition, but it is strictly a matter of style;
    the opposite is also fine, as far as the compiler is concerned.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 5, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    686
    Ron Natalie
    Oct 10, 2003
  2. Thomas Matthews
    Replies:
    5
    Views:
    2,367
    tom_usenet
    Aug 2, 2004
  3. Fraser Ross
    Replies:
    4
    Views:
    1,023
    Fraser Ross
    Aug 14, 2004
  4. pembed2012
    Replies:
    1
    Views:
    408
    James Kuyper
    Feb 27, 2012
  5. Andre
    Replies:
    5
    Views:
    517
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page