> From: splint-discuss-boun...@cs.virginia.edu [mailto:splint-discuss-
> boun...@cs.virginia.edu] On Behalf Of Wenzel, Bodo
> Sent: Wednesday, 09 September, 2009 02:00
> To: Discussions about the Splint annotation-assisted static
> analysisproject
>
> [unattributed]
> > The proper way to define a function pointer is like this,
> >
> > typedef int (*handler_h)(int c);

That's a conventional idiom for defining a function pointer, but nothing
makes it "the proper way".

> >> typedef int (handler_h)(int c);
> >>
> >> static int test(handler_h *h, char c)
>
> As I found in the standard, 'handler_h' is a defined type of a
function
> receiving an int and returning an int, not a pointer to such. And
> 'test()' is defined with the first argument being a pointer to
> 'handler_h'.

Correct, though remember that typedef does not actually define a type,
but a type alias (its name and the language in the standard
notwithstanding).

Note the standard has an example of precisely this sort of typedef:

        EXAMPLE 1 After

                typedef int MILES, KLICKSP();
                typedef struct { double hi, lo; } range;

        the constructions

                MILES distance;
                extern KLICKSP *metricp;
                range x;
                range z, *zp;

        are all valid declarations. The type of distance is int, that of
metricp is      "pointer to function with no parameter specification
returning int", ...

        (6.7.7 #4)

Also note the parentheses around the identifier "handler_h" in the
typedef definition are unnecessary.

> However, I think in either case the call of the given function should
> be
> 
>     if ((*h)())

The indirection operator and parentheses around "*h" are redundant. The
function-call operator takes an operand of type "pointer to function".
Most often this is a function identifier that decays to type
pointer-to-function, but if the identifier happens to be of type
pointer-to-function already, it's used as-is.

Thus:

        int (*h)(void);
        ...
        if (h())

is perfectly correct. Some people like to use (*h) to indicate at the
point of use that h is a function pointer, but I don't see that as a
particularly useful piece of information - if it's potentially unclear
to maintainers, add a comment. The extra asterisk and parentheses are
just visual clutter.

-- 
Michael Wojcik
Principal Software Systems Developer, Micro Focus


_______________________________________________
splint-discuss mailing list
splint-discuss@mail.cs.virginia.edu
http://www.cs.virginia.edu/mailman/listinfo/splint-discuss

Reply via email to