Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function pointer question

Reply
Thread Tools

function pointer question

 
 
Aaron Jackson
Guest
Posts: n/a
 
      09-04-2005
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.
 
Reply With Quote
 
 
 
 
Eric Laberge
Guest
Posts: n/a
 
      09-04-2005
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
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-04-2005
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;
}

 
Reply With Quote
 
Aaron Jackson
Guest
Posts: n/a
 
      09-04-2005
In article <HvKSe.6760$(E-Mail Removed). net>,
Martin Ambuhl <(E-Mail Removed)> 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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-05-2005
On Sun, 04 Sep 2005 17:41:41 -0400, Aaron Jackson <(E-Mail Removed)>
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>>
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-05-2005
On Sun, 04 Sep 2005 17:41:41 -0400, Aaron Jackson <(E-Mail Removed)>
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>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments