Benjamin Scott wrote:

> I want to define a pointer capable of holding any
> function.  How do I define it?  I've tried several
> things, but it keeps claiming the variable I'm tring
> to create isn't defined.
>
>   void (*funcaddr)();
> Am I missing a '*'?

What you have created above is a pointer to a function that accepts and
returns nothing.

>
>  void *((*funcaddr)());
> This looks more complicated, then needed.

This looks incorrect to me, but i am not sure abt this one. If u had:
void *(*funcaddr)();
then funcaddr is a ptr to a function that accepts nothing and returns a
void *. However that extra set of braces complicate things... :(

>
>   void *funcaddr();
> This looks like I'm pointing to a array of undefined
> elements.

Here u have declared a function named funcaddr which accepts nothing and
returns a void *. Please note that unless u apply appropriate braces,
the "*" applies to the return type, and not the function name.

>
>           Thanks for any help in advanced.
>

AFAIK, you cant define a function ptr that's applicable to all
functions. You can write global funcptrs which are specific to a set of
passed parameters and a return type.

Hope this helps...

Regards,

Saikat


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
----- Original Message -----
From: "Saikat Dutta" <duttas@india.hp.com>
To: <C-Guru@yahoogroups.com>
Sent: Thursday, March 18, 2004 4:42 PM
Subject: Re: [C-Guru] Thank you for the help. One more question.


> Hi,
>
> Comments inline.
>
> Benjamin Scott wrote:
>
> > I want to define a pointer capable of holding any
> > function.  How do I define it?  I've tried several
> > things, but it keeps claiming the variable I'm tring
> > to create isn't defined.
> >
> >   void (*funcaddr)();
> > Am I missing a '*'?

As far as I know, `funcaddr' should work as long as your _any_
function returns nothing.  For example,

    F:\Vijay\C> type func_ptr.c
    #include <stdio.h>
    #include <stdlib.h>

    /* __FUNCTION__ is a gcc extension, may not work
       for other compilers */
    void func  ( struct foo *foo )
    {
        puts ( __FUNCTION__ );
    }

    void func2 ( int a )
    {
        puts ( __FUNCTION__ );
    }

    void func3 ( void )
    {
        puts ( __FUNCTION__ );
    }

    int
    main ( void )
    {
        extern struct foo foo = { 0 }; /* assume the foo imported */
        void (*funcaddr) ();

        funcaddr = func;
        (*funcaddr)( &foo );
        /* This can also be called as `funcaddr ( &foo );' */

        funcaddr = func2;
        (*funcaddr)( 4 );

        funcaddr = func3;
        (*funcaddr)( );

        return EXIT_SUCCESS;
    }

    F:\Vijay\C> gcc func_ptr.c
    F:\Vijay\C> a.exe
    func
    func2
    func3

>
> What you have created above is a pointer to a function that accepts and
> returns nothing.

Your statement is partially wrong for Benjamin Scott's declaration.  It's
correct interpretation is:  `funcaddr' is a pointer to function which takes
unspecfied (number of) arguments and returns nothing.

Your interpretation applies to the declaration:
    void (*funcaddr)( void );

>
> >
> >  void *((*funcaddr)());
> > This looks more complicated, then needed.

This is equivalent to:
    void * (*funcaddr)();

As already mention by Saikat Dutta, the parantheses are unecessary.
But, mistakenly he mentioned braces instead of parantheses.  See,

[ and ] are brackets
{ and } are braces
( and ) are parantheses
< and > are angular brackets

>
> This looks incorrect to me, but i am not sure abt this one. If u had:
> void *(*funcaddr)();
> then funcaddr is a ptr to a function that accepts nothing and returns a
> void *. However that extra set of braces complicate things... :(

Again a correction here.  See my above comments.  Additionally, returning
a `void *' is too generic since it would require every function to return
a poiter to something when, in reality, that may not be needed.

>
> >
> >   void *funcaddr();
> > This looks like I'm pointing to a array of undefined
> > elements.
>

This is wrong.  `funcaddr' is no more a pointer to function.

> Here u have declared a function named funcaddr which accepts nothing and
> returns a void *. Please note that unless u apply appropriate braces,
> the "*" applies to the return type, and not the function name.
>
> >
> >           Thanks for any help in advanced.
> >
>
> AFAIK, you cant define a function ptr that's applicable to all
> functions. You can write global funcptrs which are specific to a set of
> passed parameters and a return type.

A pointer to a function of one type can be converted to a pointer to function
of another type, and back again.  But, if the converted pointer is used to call
the function of incompatible type, the behaviour is undefined.