The Dangers of sizeof
Richard Sargent
richard at pantor.UUCP
Thu Apr 13 04:48:16 AEST 1989
acu at mentor.cc.purdue.edu (Floyd McWilliams) wrote in article
<2280 at mentor.cc.purdue.edu>:
> In article <105 at servio.UUCP> penneyj at servio.UUCP (D. Jason Penney) writes:
>
> [Discussion of how "sizeof (char) - 1" is 1 not 0.]
>
> Oh really? Then take a look at this:
>
> --
> [Script to prove it ain't so.]
> --
>
> "sizeof" may not be a function, but it does have a high precedence
> (it's a unary operator). If what you posted were true, then everyone who
> tried a "sizeof(foo) + CONST" under pre-ANSI standard C would have been
> screwed; C didn't have a unary plus.
>
Quite right! I quote K&R, Section 7.2 Unary Operators, pages 187-188
(first edition). The very last paragraph of the section, on page 188,
reads:
"The construction sizeof( type ) is taken to be a unit, so the
expression sizeof( type ) - 2 is the same as (sizeof( type )) - 2."
QED.
Richard Sargent
Systems Analyst
More information about the Comp.lang.c
mailing list