oops... (was Re: Pointers to functions)

Walter Murray walterm at hpwrce.HP.COM
Sat May 18 02:30:01 AEST 1991


steve at taumet.com (Stephen Clamage) writes:

> aj3u at wilbury.cs.virginia.edu (Asim Jalis) writes:

>>What is the difference between these two (pf is a pointer to a
>>function, and hello is a function):

>>pf = hello;	
>>and 
>>pf = &hello;

> There is no difference.  The oddity is this:  A function designator
> appearing in an expression context is replaced by the address of the
> function, making a pointer-to-function.  Attempts to take the address
> of the function designator are ignored. So
> 	hello
> 	&hello
> 	&&&&&&&&&&&&&&&&hello
> are all equivalent.

I would say that an attempt to take the address of a function
designator is honored, but is unnecessary because it's a conversion that
is done automatically anyway.  

And the third example has problems.  First, && will be taken as a
logical AND operator, resulting in a syntax error.  We can correct
that by writing:

   & & & & & & & & & & & & & & & &hello;

But it seems to me this is still illegal.  When & is applied to
a function designator, the result is a pointer to a function and
is not an lvalue.  This pointer-to-function does not get converted
back to a function designator.  Applying the next & should cause a
diagnostic, because the operand of & must be either a function
designator or an lvalue, and &hello is neither.  Relevant
sections in the Standard are 3.2.2.1 and 3.3.3.2.

> Similarly, when you dereference a pointer-to-function, you get a
> function designator, which is replaced by pointer-to-function.
> Consequently,
> 	pf()
> 	(*pf)()
> 	(****************pf)()
> are all equivalent.

Right.


Walter Murray
----------



More information about the Comp.lang.c mailing list