sizeof() confusion

Frank Wales frank at grep.co.uk
Fri Nov 16 01:10:04 AEST 1990


In article <2692 at cirrusl.UUCP> dhesi%cirrusl at oliveb.ATC.olivetti.com (Rahul Dhesi) writes:
>If characters are promoted to ints in expressions, then why isn't
>|sizeof c| equivalent to |sizeof (int) c|?  The confusion arises
>because the term "expression" is defined differently in the definition
>of C and in colloquoal conversation.

Not really, since sizeof operates on objects, rather than expressions,
although the object may be defined by an expression.  For example,
  sizeof (int)c   returns the size of the (anonymous) object which is the
result of evaluating the expression  (int)c .  If   sizeof c  operated on the
result of the expression  c  rather than the object named by c, then
sizeof could not be used to find out the size of the object c itself.
Some quoting mechanism would then be needed, and things might end up
syntactically more complex than they currently are.

>From my point of view -- call it naive if you will -- anything
>that has a value is an "expression".

Okay; "it's naive". :-)  Although expressions have values, that doesn't
mean that all values are the result of an expression.  There are times
when it must be possible to ask questions about objects themselves, rather
than the values they normally evaluate to.  It just so happens that
sizeof makes this distinction through virtue of its definition.  This
is also why it must be a part of the language itself, rather than an
external function.

>Therefore, if |c| as used above
>has a value, it's an expression.  Therefore |c| must be promoted to
>int.  Therefore |sizeof c| is equivalent to |sizeof (int) c|.  Hence
>the surprise.

The logic is sound, but the premise is faulty.

>But they can only guarantee what things mean, not
>whether the meanings they define will surprise programmers.

Ah, now you're talking about the interaction of people and ideas, at
which point the psychology of programming becomes the subject, and
so we must leave comp.lang.c ...
--
Frank Wales, Grep Limited,             [frank at grep.co.uk<->uunet!grep!frank]
Kirkfields Business Centre, Kirk Lane, LEEDS, UK, LS19 7LX. (+44) 532 500303



More information about the Comp.lang.c mailing list