Ptr to func doesn't like casting??

Henry Spencer henry at utzoo.UUCP
Wed Nov 28 03:24:47 AEST 1984

> [Trying to store a char pointer in a function pointer is hard to get
> past the compiler/lint]
> 	(char *)varname = function();
> doesn't work (lvalue required).
> And,
> 	varname = (*function())();
> doesn't work consistantly.

Well, you might try:

	varname = (int(*)())function();

which is the correct cast form -- get the value from the function and
then cast it to pointer-to-int-function.

However, I am morally bound to tell you that you are sinning :-), and in
a somewhat unportable way.  Pointers to functions and pointers to data
are very different animals, and on some machines they are of different
sizes.  Using one to hold the other is hazardous.  I believe the ANSI C
draft outlaws such conversions altogether, although I haven't got my
copy handy to check.  Use a union; it's much more portable, and should
shut the compilers up as a useful bonus.
				Henry Spencer @ U of Toronto Zoology

More information about the Comp.lang.c mailing list