Pointers to functions

Discussion in 'C Programming' started by Mr John FO Evans, Mar 25, 2006.

  1. Hi

    Have already used function pointers to a simple subroutine ie

    typedef void (*FPOINT)(void)
    void func(void)
    typedef struct
    {
    FPOINT func;
    ....
    } name;

    However when I try to do the same with:-

    double func(int a, double b.....etc);

    I get into a mess when I wish to equivalence two functions with:-

    typedef union
    {
    FPOINT funca;
    FPOINT funcb;
    }joint;

    even when I use
    typedef double (*FPOINT)(inta, double b......)

    Can anyone help a novice function pointer man!!

    John




    --
    _ _________________________________________
    / \._._ |_ _ _ /' Orpheus Internet Services
    \_/| |_)| |(/_|_|_> / 'Internet for Everyone'
    _______ | ___________./ http://www.orpheusinternet.co.uk
     
    Mr John FO Evans, Mar 25, 2006
    #1
    1. Advertising

  2. Mr John FO Evans

    Michael Mair Guest

    Mr John FO Evans schrieb:
    > Hi
    >
    > Have already used function pointers to a simple subroutine ie
    >
    > typedef void (*FPOINT)(void)
    > void func(void)
    > typedef struct
    > {
    > FPOINT func;
    > ....
    > } name;
    >
    > However when I try to do the same with:-
    >
    > double func(int a, double b.....etc);
    >
    > I get into a mess when I wish to equivalence two functions with:-
    >
    > typedef union
    > {
    > FPOINT funca;
    > FPOINT funcb;
    > }joint;


    A union most of the time is used to collect equivalently used
    mutually exclusive members of different type.
    You probably want
    typedef union {
    FPOINT funca;
    FPOINT2 funcb;
    }
    where FPOINT2 is a different function pointer type.

    > even when I use
    > typedef double (*FPOINT)(inta, double b......)


    Use
    typedef double (*FPOINT2) (int, double, ....);

    You still have the problem of determining when to use funca
    and when to use funcb.

    A recent thread starting at
    <>
    discusses some dos and donts and alternatives to the above
    union.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Mar 25, 2006
    #2
    1. Advertising

  3. On Sat, 25 Mar 2006 11:34:14 GMT, Mr John FO Evans
    <> wrote:

    >Hi
    >
    >Have already used function pointers to a simple subroutine ie
    >
    >typedef void (*FPOINT)(void)
    >void func(void)
    >typedef struct
    > {
    > FPOINT func;
    > ....
    > } name;
    >
    >However when I try to do the same with:-
    >
    >double func(int a, double b.....etc);

    The type FPOINT describes a pointer to a function which accepts no
    arguments and does not return anything. An object of this type should
    never hold the address of a function of any other type. Your new
    function returns a double and accepts arguments. The address of this
    function should NOT be stored in an object of type FPOINT.


    Remove del for email
     
    Barry Schwarz, Mar 25, 2006
    #3
  4. Mr John FO Evans

    santosh Guest

    Mr John FO Evans wrote:
    > Hi
    >
    > Have already used function pointers to a simple subroutine ie
    >
    > typedef void (*FPOINT)(void)
    > void func(void)
    > typedef struct
    > {
    > FPOINT func;
    > ....
    > } name;
    >
    > However when I try to do the same with:-
    >
    > double func(int a, double b.....etc);
    >
    > I get into a mess when I wish to equivalence two functions with:-
    >
    > typedef union
    > {
    > FPOINT funca;
    > FPOINT funcb;
    > }joint;
    >
    > even when I use
    > typedef double (*FPOINT)(inta, double b......)


    FPOINT is already typedef'ed as a pointer to a function. Then you're
    coercing it to act as a pointer to a function of a different signature.
    Calling the function will invoke undefined behaviour. Instead declare
    another type to point to the second function.
     
    santosh, Mar 25, 2006
    #4
  5. Mr John FO Evans <> writes:
    > Have already used function pointers to a simple subroutine ie
    >
    > typedef void (*FPOINT)(void)
    > void func(void)
    > typedef struct
    > {
    > FPOINT func;
    > ....
    > } name;
    >
    > However when I try to do the same with:-
    >
    > double func(int a, double b.....etc);
    >
    > I get into a mess when I wish to equivalence two functions with:-
    >
    > typedef union
    > {
    > FPOINT funca;
    > FPOINT funcb;
    > }joint;
    >
    > even when I use
    > typedef double (*FPOINT)(inta, double b......)
    >
    > Can anyone help a novice function pointer man!!


    What exactly are you trying to accomplish? Please explain without
    trying to use "equivalence" as a verb. Verbing nouns isn't
    necessarily a bad thing; I just don't know what you mean.

    Why are you using a union? If you expect to be able to store a value
    in one member of a union and then use the other one, you're probably
    doing something wrong. You need to keep track of which member is
    currently valid (only one at a time can be), unless you're
    deliberately doing some very low-level and non-portable stuff.

    A good start would be to show us some actual code. Don't use "..."
    unless it's actually part of the function declaration.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 25, 2006
    #5
  6. Mr John FO Evans

    Default User Guest

    Mr John FO Evans wrote:


    > I get into a mess when I wish to equivalence two functions with:-
    >
    > typedef union
    > {
    > FPOINT funca;
    > FPOINT funcb;
    > }joint;


    What are you trying to accomplish, what problem are you solving? Your
    approach, as the others have pointed out, is fundamentally flawed. I
    can't really grasp WHY you want to do this. Explain your goal as well
    as your broken solutions in these cases. Very likely the true answer
    lies elsewhere.



    Brian
     
    Default User, Mar 25, 2006
    #6
  7. In article <>, Keith Thompson <>
    wrote:
    > > However when I try to do the same with:-
    > >
    > > double func(int a, double b.....etc);
    > >
    > > I get into a mess when I wish to equivalence two functions with:-
    > >
    > > typedef union
    > > {
    > > FPOINT funca;
    > > FPOINT funcb;
    > > }joint;
    > >
    > > even when I use
    > > typedef double (*FPOINT)(inta, double b......)
    > >
    > > Can anyone help a novice function pointer man!!

    >
    > What exactly are you trying to accomplish? Please explain without
    > trying to use "equivalence" as a verb. Verbing nouns isn't
    > necessarily a bad thing; I just don't know what you mean.
    >
    > Why are you using a union? If you expect to be able to store a value
    > in one member of a union and then use the other one, you're probably
    > doing something wrong. You need to keep track of which member is
    > currently valid (only one at a time can be), unless you're
    > deliberately doing some very low-level and non-portable stuff.
    >

    I was attempting to allow a function to have effectively two entry points
    (easy in assembler) so that it could be called from either. I also need to
    disable one entry point or switch entry points using a compiler directive.

    The current code (which is fine) is:-

    double funca(<args>) /* args is always the same */
    {
    '''''
    return <double>;
    }

    #if NOT_EMBEDDED
    double func(<args>)
    {
    return funca(<args>);
    }
    #endif

    If a number of code fragments as above are embedded in a single task that task
    can call funca (or funcb etc). The Linker requires that the unused entry point
    func (which will be in every fragment) is disabled in this case.

    Alternatively each function may be embedded in a single task - in which case the
    call to the function always needs to be func. In this case I was hoping to avoid
    the double call with a (long) argument string by using some form of equivalence
    or a function pointer.

    NB the value of NOT_EMBEDDED is set in the call to the compiler (Norcroft)

    NNB I apologise if my terminology is a bit shaky. In this case task means
    a separate program.


    John


    --
    _ _________________________________________
    / \._._ |_ _ _ /' Orpheus Internet Services
    \_/| |_)| |(/_|_|_> / 'Internet for Everyone'
    _______ | ___________./ http://www.orpheusinternet.co.uk
     
    Mr John FO Evans, Mar 25, 2006
    #7
  8. Mr John FO Evans

    Eric Sosman Guest

    Mr John FO Evans wrote On 03/25/06 18:18,:
    > In article <>, Keith Thompson <>
    > wrote:
    >
    >>>However when I try to do the same with:-
    >>>
    >>>double func(int a, double b.....etc);
    >>>
    >>>I get into a mess when I wish to equivalence two functions with:-
    >>>
    >>>typedef union
    >>> {
    >>> FPOINT funca;
    >>> FPOINT funcb;
    >>> }joint;
    >>>
    >>>even when I use
    >>>typedef double (*FPOINT)(inta, double b......)
    >>>
    >>>Can anyone help a novice function pointer man!!

    >>
    >>What exactly are you trying to accomplish? Please explain without
    >>trying to use "equivalence" as a verb. Verbing nouns isn't
    >>necessarily a bad thing; I just don't know what you mean.
    >>
    >>Why are you using a union? If you expect to be able to store a value
    >>in one member of a union and then use the other one, you're probably
    >>doing something wrong. You need to keep track of which member is
    >>currently valid (only one at a time can be), unless you're
    >>deliberately doing some very low-level and non-portable stuff.
    >>

    >
    > I was attempting to allow a function to have effectively two entry points
    > (easy in assembler) so that it could be called from either. I also need to
    > disable one entry point or switch entry points using a compiler directive.
    >
    > The current code (which is fine) is:-
    >
    > double funca(<args>) /* args is always the same */
    > {
    > '''''
    > return <double>;
    > }
    >
    > #if NOT_EMBEDDED
    > double func(<args>)
    > {
    > return funca(<args>);
    > }
    > #endif
    >
    > If a number of code fragments as above are embedded in a single task that task
    > can call funca (or funcb etc). The Linker requires that the unused entry point
    > func (which will be in every fragment) is disabled in this case.
    >
    > Alternatively each function may be embedded in a single task - in which case the
    > call to the function always needs to be func. In this case I was hoping to avoid
    > the double call with a (long) argument string by using some form of equivalence
    > or a function pointer.
    >
    > NB the value of NOT_EMBEDDED is set in the call to the compiler (Norcroft)
    >
    > NNB I apologise if my terminology is a bit shaky. In this case task means
    > a separate program.


    It sounds as if you just want to change the name
    of the function depending on whether it's "embedded"
    or not (whatever that means). If so, won't

    #if NOT_EMBEDDED
    #define funca func
    #endif

    double funca(...) { ... }

    do the trick?

    --
     
    Eric Sosman, Mar 27, 2006
    #8
  9. In article <e09c6d$5o5$>, Eric Sosman
    <> wrote:
    >
    >
    >
    > Mr John FO Evans wrote On 03/25/06 18:18,:
    > > In article <>, Keith Thompson

    > <>
    > > wrote:
    > >
    > >>>However when I try to do the same with:-
    > >>>
    > >>>double func(int a, double b.....etc);
    > >>>
    > >>>I get into a mess when I wish to equivalence two functions with:-
    > >>>
    > >>>typedef union
    > >>> {
    > >>> FPOINT funca;
    > >>> FPOINT funcb;
    > >>> }joint;
    > >>>
    > >>>even when I use
    > >>>typedef double (*FPOINT)(inta, double b......)
    > >>>
    > >>>Can anyone help a novice function pointer man!!
    > >>
    > >>What exactly are you trying to accomplish? Please explain without
    > >>trying to use "equivalence" as a verb. Verbing nouns isn't
    > >>necessarily a bad thing; I just don't know what you mean.
    > >>
    > >>Why are you using a union? If you expect to be able to store a value
    > >>in one member of a union and then use the other one, you're probably
    > >>doing something wrong. You need to keep track of which member is
    > >>currently valid (only one at a time can be), unless you're
    > >>deliberately doing some very low-level and non-portable stuff.
    > >>

    > >
    > > I was attempting to allow a function to have effectively two entry

    > points
    > > (easy in assembler) so that it could be called from either. I also need

    > to
    > > disable one entry point or switch entry points using a compiler

    > directive.
    > >
    > > The current code (which is fine) is:-
    > >
    > > double funca(<args>) /* args is always the same */
    > > {
    > > '''''
    > > return <double>;
    > > }
    > >
    > > #if NOT_EMBEDDED
    > > double func(<args>)
    > > {
    > > return funca(<args>);
    > > }
    > > #endif
    > >
    > > If a number of code fragments as above are embedded in a single task

    > that
    > task
    > > can call funca (or funcb etc). The Linker requires that the unused entry

    > point
    > > func (which will be in every fragment) is disabled in this case.
    > >
    > > Alternatively each function may be embedded in a single task - in which

    > case the
    > > call to the function always needs to be func. In this case I was hoping

    > to
    > avoid
    > > the double call with a (long) argument string by using some form of

    > equivalence
    > > or a function pointer.
    > >
    > > NB the value of NOT_EMBEDDED is set in the call to the compiler

    > (Norcroft)
    > >
    > > NNB I apologise if my terminology is a bit shaky. In this case task

    > means
    > > a separate program.

    >
    > It sounds as if you just want to change the name
    > of the function depending on whether it's "embedded"
    > or not (whatever that means). If so, won't
    >
    > #if NOT_EMBEDDED
    > #define funca func
    > #endif
    >
    > double funca(...) { ... }
    >
    > do the trick?
    >

    Yes of course - it works.

    Thanks a lot

    John

    PS By embedded I simply meant included in the main program and not a
    separate program exchanging data with the main.

    In this application it is convenient to test the function in a separate
    program before building it into the main program - even more convenient if
    the function code can be identical in both environments.




    --
    _ _________________________________________
    / \._._ |_ _ _ /' Orpheus Internet Services
    \_/| |_)| |(/_|_|_> / 'Internet for Everyone'
    _______ | ___________./ http://www.orpheusinternet.co.uk
     
    Mr John FO Evans, Mar 27, 2006
    #9
    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. S?ren Gammelmark
    Replies:
    1
    Views:
    1,984
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    883
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    326
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    729
  5. ajaybgr
    Replies:
    18
    Views:
    1,085
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page