By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

function pointer to extern?

P: n/a
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.

Jun 4 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Jun 4 '07 #2

P: n/a
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.

Jun 4 '07 #3

P: n/a
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.
Jun 4 '07 #4

P: n/a
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.

Jun 4 '07 #5

P: n/a
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.
Jun 4 '07 #6

P: n/a
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.
Jun 5 '07 #7

P: n/a
On Jun 4, 9:26 pm, Haro Panosyan <h...@ti.comwrote:

[...]
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:ja*********@gmail.com
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

Jun 5 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.