Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

function pointer to extern?


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

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.

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

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

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.

On Jun 4, 9:26 pm, Haro Panosyan <h@ti.com> 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:james.ka@gmail.com
Conseils en informatique oriente objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc