function pointer to extern?

T

Travis

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

Haro Panosyan

See my insetrs bellow:
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.
 
V

Victor Bazarov

Travis said:
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
 
R

Rolf Magnus

Travis said:
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.
 
M

Mark P

Travis said:
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
 
T

Travis

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

Mark P

Travis said:
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.
 
J

James Kanze

[...]
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top