Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   function pointer question (http://www.velocityreviews.com/forums/t439299-function-pointer-question.html)

Aaron Jackson 09-04-2005 09:41 PM

function pointer question
 
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.

Eric Laberge 09-04-2005 09:51 PM

Re: function pointer question
 
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

Martin Ambuhl 09-04-2005 10:25 PM

Re: function pointer question
 
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;
}


Aaron Jackson 09-04-2005 11:37 PM

Re: function pointer question
 
In article <HvKSe.6760$9i4.4335@newsread2.news.atl.earthlink. net>,
Martin Ambuhl <mambuhl@earthlink.net> 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

Barry Schwarz 09-05-2005 07:27 PM

Re: function pointer question
 
On Sun, 04 Sep 2005 17:41:41 -0400, Aaron Jackson <none@nospam.com>
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 09-05-2005 09:05 PM

Re: function pointer question
 
On Sun, 04 Sep 2005 17:41:41 -0400, Aaron Jackson <none@nospam.com>
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>>


All times are GMT. The time now is 04:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.