A
Aaron Jackson
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.
#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.