[comp.lang.c] Typecast of Pointers to Functions

norvell@csri.toronto.edu (Theodore Stevens Norvell) (03/29/89)

In article <7689@killer.Dallas.TX.US> brian asks:
>I am looking for an equivalent way to specify a typecast of a pointer to 
>a function other than using typedef.  For Example:
>  int         (*fcn_ptr2) (); 
>
>  fcn_ptr2 = (???????) NULL;  /* Do Not Use the typedef.  What Goes Here? */

Try   fcn_ptr2 = (int (*)()) NULL ;

The basic rule for type casts is that they look the same as a declaration
of a single identifier except (a) no semi-colon, (b) parentheses around it,
and (c) no identifier.  So it's really very simple.

To read a cast, just figure out where the only spot the identifier could
go is and then its understood the same as you understand declarations.

There is one more rule which says that parentheses that are in the cast,
but aren't used to indicate a function, must contain something.  This
rule is to avoid ambiguity and to make people who understand it feel
better.  E.g., this is a valid declaration for a pointer to a char
	char *(pc) ;
but _this_ is not going to cast to that type
	(char *())
but rather to a function returning a pointer to a char.  Without the
final rule an identifier could go in the parentheses, but with the
rule, it could only go after the *. So it's not all that simple after
all.

By the way.  In most versions of C (including ANSI), you shouldn't _need_
to cast NULL when you assign it, since the compiler can figure out what
to do.  

Theodore Norvell

brian@killer.Dallas.TX.US (Brian Pellerin) (03/29/89)

I am looking for an equivalent way to specify a typecast of a pointer to 
a function other than using typedef.  For Example:

  typedef int (*FCN_PTR) ();
  FCN_PTR     fcn_ptr1;
  int         (*fcn_ptr2) (); 

  fcn_ptr1 = (FCN_PTR) NULL;  /* Typecast using typedef */
  fcn_ptr2 = (???????) NULL;  /* Do Not Use the typedef.  What Goes Here? */

Any Suggestions?  Thanks.


...uunet!killer!brian

hearn@claris.com (Bob Hearn) (03/29/89)

From article <7689@killer.Dallas.TX.US>, by brian@killer.Dallas.TX.US (Brian Pellerin):
> I am looking for an equivalent way to specify a typecast of a pointer to 
> a function other than using typedef.  For Example:
> 
>   typedef int (*FCN_PTR) ();
>   FCN_PTR     fcn_ptr1;
>   int         (*fcn_ptr2) (); 
> 
>   fcn_ptr1 = (FCN_PTR) NULL;  /* Typecast using typedef */
>   fcn_ptr2 = (???????) NULL;  /* Do Not Use the typedef.  What Goes Here? */
> 
> Any Suggestions?  Thanks.

What you want is:

fcn_ptr2 = (int (*)()) NULL;

The parens around the * are significant!  There is a simple way to to this
for any type: write a declaration, remove the identifier, and enclose it in
parens.  Thus:

int (*FCN_PTR)()    -->    int (*)()    -->   (int (*)())

Bob Hearn
hearn@claris.com

chris@mimsy.UUCP (Chris Torek) (03/29/89)

In article <7689@killer.Dallas.TX.US> brian@killer.Dallas.TX.US
(Brian Pellerin) writes:
>I am looking for an equivalent way to specify a typecast of a pointer to 
>a function other than using typedef.  For Example:
>
>  int         (*fcn_ptr2) (); 
>
>  fcn_ptr2 = (???????) NULL;  /* Do Not Use the typedef.  What Goes Here? */

The cast is not strictly necessary here, but its form is

	fcn_ptr2 = (int (*)()) NULL;

See any half-decent C book for the derivation.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

crossgl@ingr.com (Gordon Cross) (03/29/89)

In article <7689@killer.Dallas.TX.US> brian@killer.DALLAS.TX.US (Brian Pellerin) writes:
}I am looking for an equivalent way to specify a typecast of a pointer to 
}a function other than using typedef.  For Example:
}
}  typedef int (*FCN_PTR) ();
}  FCN_PTR     fcn_ptr1;
}  int         (*fcn_ptr2) (); 
}
}  fcn_ptr1 = (FCN_PTR) NULL;  /* Typecast using typedef */
}  fcn_ptr2 = (???????) NULL;  /* Do Not Use the typedef.  What Goes Here? */


Sure--

   fcn_ptr2 = (int (*)()) NULL;
-- 

Gordon Cross             UUCP:      uunet!ingr!crossgl     "all opinions are
111 Westminister Way     INTERNET:  crossgl@ingr.com        mine and not those
Madison, AL 35758        MA BELL:   (205) 772-7842          of my employer."

john@chinet.chi.il.us (John Mundt) (03/29/89)

In article <7689@killer.Dallas.TX.US> brian@killer.DALLAS.TX.US (Brian Pellerin) writes:
>I am looking for an equivalent way to specify a typecast of a pointer to 
>a function other than using typedef.  For Example:
>
>  typedef int (*FCN_PTR) ();
>  FCN_PTR     fcn_ptr1;
>  int         (*fcn_ptr2) (); 
>
>  fcn_ptr1 = (FCN_PTR) NULL;  /* Typecast using typedef */
>  fcn_ptr2 = (???????) NULL;  /* Do Not Use the typedef.  What Goes Here? */

What you need is the following cast

	fcn_ptr2 = (int (*)()) NULL;

If you look at your typedef, you'll see that this is virtually the same thing
with the name taken out and with the casting parentheses put around the 
entire thing.

Why not use 0 instead of NULL, too?



-- 
---------------------
John Mundt   Teachers' Aide, Inc.  P.O. Box 1666  Highland Park, IL
john@chinet.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  

ftw@masscomp.UUCP (Farrell Woods) (03/30/89)

In article <7689@killer.Dallas.TX.US> brian@killer.DALLAS.TX.US (Brian Pellerin) writes:
>I am looking for an equivalent way to specify a typecast of a pointer to 
>a function other than using typedef.  For Example:

[example deleted]

>Any Suggestions?  Thanks.

Easy.  Try this:

	fcn_ptr2 = (int (*)())0;

Note that I did not use the NULL macro, since it is not always correctly
defined.
-- 
Farrell T. Woods				Voice:  (508) 392-2471
Concurrent Computer Corporation			Domain: ftw@masscomp.com
1 Technology Way				uucp:   {backbones}!masscomp!ftw
Westford, MA 01886				OS/2:   Half an operating system