Re: function pointers as function parameters
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 03 May 2005 20:41:55 GMT
Eric Sosman <eric.sosman@xxxxxxx> writes:
[...]
> Although it's flattering to learn that someone agrees with
> me, it is also a bit embarrassing -- because I have come to
> *dis*agree with myself ;-)
>
> I said there was no context in which a function identifier
> doesn't "decay" to a pointer to the function, but Keith
> Thompson has convinced me I was wrong. Specifically:
>
> - In the context `&func', the `func' part does not behave
> like a pointer-to-function. If it did, the entire
> expression would be pointer-to-pointer-to-function --
> but, strangely enough, both `func' and `&func' (and
> even `&&&&&&&func') are pointer-to-function.
As Lawrence Kirby pointed out, this is illegal because the "maximal
munch" rule turns this into three "&&" operators followed by an "&"
operator; once you fix that, it's still illegal because &func isn't a
lvalue. But you can have &func, func, *func, **func, *****func, and
*&*&*&*&func.
> - In the context `sizeof func', the `func' part does not
> behave like a pointer-to-function. If it did, then
> expression would evaluate to the size of a function
> pointer, but in actuality the expression produces a
> diagnostic.
>
> The second point is debatable: since the context is not
> valid C at all, its odd behavior doesn't matter. One might
> equally well make arguments about the array-indexing operator
> in the context `[4]"Hello"'; interesting things might be said,
> but they'd have little to do with C. But even if one rejects
> the second point, the first seems unassailable: Keith is right
> and I was wrong, R-O-N-G, wrong.
I hate to force you to disagree with yourself even further, but here's
what the standard says:
C99 6.3.2.1p4:
A function designator is an expression that has function
type. Except when it is the operand of the sizeof operator(54) or
the unary & operator, a function designator with type "function
returning type" is converted to an expression that has type
"pointer to function returning type".
And footnote (54):
Because this conversion does not occur, the operand of the sizeof
operator remains a function designator and violates the constraint
in 6.5.3.4.
6.5.3.4p1 says:
Constraints
The sizeof operator shall not be applied to an expression that has
function type or an incomplete type, to the parenthesized name of
such a type, or to an expression that designates a bit-field
member.
It seems a little convoluted to make an exception to the rule for the
operand of a sizeof operator, only to make it a constraint violation
in a different section of the standard, but I think this is actually
the most straightforward way to do it. The rule is similar to the
rule for arrays, except that it makes sense to apply sizeof to an
array value.
[4]"Hello", on the other hand, is a syntax error, not a constraint
violation. (And as you know, 4["Hello"] is perfectly legal.)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- function pointers as function parameters
- From: Marlene Stebbins
- Re: function pointers as function parameters
- From: Eric Sosman
- Re: function pointers as function parameters
- From: pete
- Re: function pointers as function parameters
- From: Lawrence Kirby
- Re: function pointers as function parameters
- From: pete
- Re: function pointers as function parameters
- From: Eric Sosman
- function pointers as function parameters
- Prev by Date: Re: How to get file size?
- Next by Date: Re: typedef struct
- Previous by thread: Re: function pointers as function parameters
- Next by thread: Re: function pointers as function parameters
- Index(es):
Relevant Pages
|