Function-types: compatability, and typedefs

Kers kers at hpld.hpl.hp.com
Fri Sep 1 06:33:40 AEST 1989


Two questions.

First, about the compatability of function types in pANS C. Here's
an (fragmentary) example that illustrates my problem.

    #include <stdlib.h>
    #include <string.h>

   ...
	qsort( Base, NMembers, Size, strcmp );
   ...

Assuming that the Base, NMembers, and Size arguments are OK, is strcmp suitable
for passing as an argument

    - without explicit casting?
    - with explicit casting?
    - ever?

*Or* do I have to write my own function (with two void* arguments), which
operates by calling strcmp after suitable casting, and hand that in? The issue
has arisen in some code we are writing, where generic procedures abound
(for example, a hash table packages that allows a table to be created with
particular hashing & comparison operations passed in), and we'd like to 
preserve type information "as long as possible" - ie, not declare things as 
accepting void* parameters just because we're going to pass them to generic
procedures.

Second, about typedefs for function types. I'd like to be able to declare a
type for a function, so that I can use it for declaring arguments. I'm a
little unclear about how I do so.

    typedef char * (char *) MonadicStringFunction;

As I read the pANS (DEC 1988, Section 3.5.5), that would be OK; the type's a
function declarator with the name omitted. (The compiler I'm using has a
problem with this at the moment, but I'd like to understand the pANS, and
the compiler at home may have a different tale to tell.)

Assuming that I can get a suitable typedef set up, I'd then like to declare
parameters with that type:

    extern int example( MonadicStringFunction arg );

Would I need to add the * to ensure the argument is treated as a function 
pointer, or is this one of the places where the lattitude about functions and
pointers applies? (I'm quite happy to add the * here).

Can I also use the typedef in a *definition*:

    MonadicStringFunction boring( char *arg ) { return arg; }

so that the compiler can catch me out if I make a type error?

Although I have access to a copy of the pANS, I'm still trying to navigate
my way round it; K&R II doesn't give enough detail to answer my questions
(or, if it does, I've missed it several times). I'd appreciate it if
someone could answer my questions, either directly, or by pointing me to
the pertinent section of the pANS.

Finally, I was going to post this to comp.std.c, but decided it wasn't
discussion "about" the standard. Would that have been a more appropriate
place?
--

Regards, Kers.
"If anything anyone lacks, they'll find it all ready in stacks."



More information about the Comp.lang.c mailing list