function pointer question

Discussion in 'C Programming' started by Aaron Jackson, Sep 4, 2005.

  1. I'm trying to use function pointers inside a union. For example:

    #include <math.h>
    #include <stdio.h>

    typedef union {
    double value;
    double (*fp)();
    } symbol_t;

    int
    main(void) {
    symbol_t symbol;

    symbol = (symbol_t)fabs;
    printf("%f\n", symbol.fp(-0.7854));
    return(0);
    }

    When I try to compile this, I get the following error:

    jackson% gcc -o test test.c
    test.c: In function `main':
    test.c:13: error: cast to union type from type not present in union

    However, when I try to use any other math lib function that returns
    double (sin for instance), the above code works. Looking though math.h,
    I don't see any difference between sin and fabs:

    extern double fabs( double );
    extern double sin( double );

    Could somebody please give me a hint as to what I am doing wrong?
    Thanks.

    Aaron

    PS I think I learned my lesson from my last post and the above code is
    more acceptable (at least its formatting). BTW, thanks to those who
    answered my last post.
     
    Aaron Jackson, Sep 4, 2005
    #1
    1. Advertising

  2. Aaron Jackson

    Eric Laberge Guest

    Aaron Jackson wrote:

    > I'm trying to use function pointers inside a union. For example:
    >
    > #include <math.h>
    > #include <stdio.h>
    >
    > typedef union {
    > double value;
    > double (*fp)();
    > } symbol_t;
    >
    > int
    > main(void) {
    > symbol_t symbol;
    >
    > symbol = (symbol_t)fabs;
    > printf("%f\n", symbol.fp(-0.7854));
    > return(0);
    > }
    >


    Change
    symbol = (symbol_t)fabs;
    to
    symbol.fp = fabs;

    and it will be OK. symbol_t.fp is already of type double(*)(), so you'll be
    able to assign fabs [which is of type double(*)(double)] correctly.

    I think it would be better if you had defined fp as double(*fp)(double)
    instead too, as I believe empty parentheses means an unspecified number of
    arguments.

    Hope this helps,
    --
    Eric Laberge
     
    Eric Laberge, Sep 4, 2005
    #2
    1. Advertising

  3. Aaron Jackson wrote:
    > I'm trying to use function pointers inside a union. For example:
    >
    > #include <math.h>
    > #include <stdio.h>
    >
    > typedef union {
    > double value;
    > double (*fp)();
    > } symbol_t;
    >
    > int
    > main(void) {
    > symbol_t symbol;
    >
    > symbol = (symbol_t)fabs;

    ^^^^^^^^^^ casting to a union is not allowed.
    > printf("%f\n", symbol.fp(-0.7854));
    > return(0);
    > }
    >
    > When I try to compile this, I get the following error:
    >
    > jackson% gcc -o test test.c
    > test.c: In function `main':
    > test.c:13: error: cast to union type from type not present in union
    >
    > However, when I try to use any other math lib function that returns
    > double (sin for instance), the above code works.


    You don't have your warnings turned on. gcc may allow it, but in ISO C
    you cannot cast to a union type. Turn on your warning diagnostics and
    set the standard to be something other than the default (which is "Gnu C").
    What you are trying to do should be done:
    #include <math.h>
    #include <stdio.h>

    typedef union
    {
    double value;
    double (*fp) ();
    } symbol_t;

    int main(void)
    {
    symbol_t symbol;

    symbol.fp = fabs;
    printf("%f\n", symbol.fp(-0.7854));
    symbol.fp = sin;
    printf("%f\n", symbol.fp(-0.7854));
    return 0;
    }
     
    Martin Ambuhl, Sep 4, 2005
    #3
  4. In article <HvKSe.6760$>,
    Martin Ambuhl <> wrote:

    > Aaron Jackson wrote:
    > > I'm trying to use function pointers inside a union. For example:
    > >
    > > #include <math.h>
    > > #include <stdio.h>
    > >
    > > typedef union {
    > > double value;
    > > double (*fp)();
    > > } symbol_t;
    > >
    > > int
    > > main(void) {
    > > symbol_t symbol;
    > >
    > > symbol = (symbol_t)fabs;

    > ^^^^^^^^^^ casting to a union is not allowed.


    OK, thanks. I see that using ``-ansi -pedantic'' causes the compiler to
    choke on my code. It is strange that my code worked with all of the
    other math functions except fabs... I guess I need to be more careful
    with the compile options I use.

    Aaron
     
    Aaron Jackson, Sep 5, 2005
    #4
  5. On Sun, 04 Sep 2005 17:41:41 -0400, Aaron Jackson <>
    wrote:

    >I'm trying to use function pointers inside a union. For example:
    >
    >#include <math.h>
    >#include <stdio.h>
    >
    >typedef union {
    > double value;
    > double (*fp)();


    In addition to the other advice you have received, you should change
    this to
    double (*fp)(double);

    You want the compiler to warn you if assign the address of the wrong
    function type to fp.

    >} symbol_t;
    >
    >int
    >main(void) {
    > symbol_t symbol;
    >
    > symbol = (symbol_t)fabs;
    > printf("%f\n", symbol.fp(-0.7854));
    > return(0);
    >}
    >
    >When I try to compile this, I get the following error:
    >
    >jackson% gcc -o test test.c
    >test.c: In function `main':
    >test.c:13: error: cast to union type from type not present in union
    >
    >However, when I try to use any other math lib function that returns
    >double (sin for instance), the above code works. Looking though math.h,
    >I don't see any difference between sin and fabs:
    >
    >extern double fabs( double );
    >extern double sin( double );
    >
    >Could somebody please give me a hint as to what I am doing wrong?
    >Thanks.
    >
    >Aaron
    >
    >PS I think I learned my lesson from my last post and the above code is
    >more acceptable (at least its formatting). BTW, thanks to those who
    >answered my last post.



    <<Remove the del for email>>
     
    Barry Schwarz, Sep 5, 2005
    #5
  6. On Sun, 04 Sep 2005 17:41:41 -0400, Aaron Jackson <>
    wrote:

    >I'm trying to use function pointers inside a union. For example:
    >
    >#include <math.h>
    >#include <stdio.h>
    >
    >typedef union {
    > double value;
    > double (*fp)();


    In addition to the other advice you have received, you should change
    this to
    double (*fp)(double);

    You want the compiler to warn you if assign the address of the wrong
    function type to fp.

    >} symbol_t;
    >
    >int
    >main(void) {
    > symbol_t symbol;
    >
    > symbol = (symbol_t)fabs;
    > printf("%f\n", symbol.fp(-0.7854));
    > return(0);
    >}
    >
    >When I try to compile this, I get the following error:
    >
    >jackson% gcc -o test test.c
    >test.c: In function `main':
    >test.c:13: error: cast to union type from type not present in union
    >
    >However, when I try to use any other math lib function that returns
    >double (sin for instance), the above code works. Looking though math.h,
    >I don't see any difference between sin and fabs:
    >
    >extern double fabs( double );
    >extern double sin( double );
    >
    >Could somebody please give me a hint as to what I am doing wrong?
    >Thanks.
    >
    >Aaron
    >
    >PS I think I learned my lesson from my last post and the above code is
    >more acceptable (at least its formatting). BTW, thanks to those who
    >answered my last post.



    <<Remove the del for email>>
     
    Barry Schwarz, Sep 5, 2005
    #6
    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. glen stark
    Replies:
    2
    Views:
    728
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,094
    Fraser Ross
    Aug 14, 2004
  3. Replies:
    10
    Views:
    742
    Chris Torek
    Feb 4, 2005
  4. murgan
    Replies:
    6
    Views:
    4,949
    Thad Smith
    Dec 21, 2005
  5. Vijai Kalyan
    Replies:
    4
    Views:
    746
    Vijai Kalyan
    Nov 8, 2005
Loading...

Share This Page