function pointer question

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.
 
E

Eric Laberge

Aaron said:
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,
 
M

Martin Ambuhl

Aaron said:
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;
}
 
A

Aaron Jackson

Martin Ambuhl said:
^^^^^^^^^^ 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
 
B

Barry Schwarz

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>>
 
B

Barry Schwarz

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>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top