[comp.lang.c] Declaration puzzler + more questions

sjs@jcricket.ctt.bellcore.com (Stan Switzer) (10/10/88)

In article <30091@oliveb.olivetti.com> chase@orc.olivetti.com
(David Chase) writes:
> I can't figure out how to declare the type of an array with (say) 10
> elements, each of which is that array type.  That is, I want the
                          [ ^ a pointer to ]
> non-error effect of
> 
> 	typedef foo *(foo[10]);
> 

No can do.  While such a type is more interesting than useful, the
same thing occurs with a function returning a pointer to the same kind
of function.  That is, what "typedef foo (*foo)();" would mean if it
meant anything.  This was discussed several months ago.  The tentative
conclusion was that this type is indeed useful, but that there
are workarounds and that since any obvious way of allowing such a
declaration would be a major change to C, it is not worth fixing.

I once heard someone criticize "Pascal" for allowing the useless type
"type foo: ^foo;" (my Pascal is rusty).  I asked him if he thought
Peano integers were useless too.  Well, OK, again maybe more
interesting than useful (in a programming language).

Now some related questions:

I want to define a function to which a user can optionally pass a
pointer to a void function.  Does the dpANS sanction a notion of a
null pointer to a function?  Can I compare (for [in]equality) two
function pointers of the same type?  My reading of the draft says
"yes."

Anything wrong with the following?

void test( void (*arg)(int a) ), int i ) {
	if ( arg ) arg( i );
}
void bar () {
	extern void foo( int a );
	test( foo, 1 );
	test( (void (*)())0, 1 );	/* a null pointer to a function
					 * returning nothing? */
}

Still another question:

Are "if ( x ) ..." and "if ( (x) != 0 ) ..." completely equivalent?
I always thought so, but I wonder.... 

And now a trick question:

Is the following valid for any valid typedef "T"?

typedef ... T ...;
T foo()
{
	extern T bar();
	return bar();
}

In other words, can a function always return an expression of the same
type as the calling function.

I think the answer is "no."  Try "typedef void T;" 

-----------------------------------------------------------------
The wreckless can conquer space.  Let the | Stan Switzer
rest of us take proper care of the Earth. | sjs@ctt.bellcore.com