structured assembler (BASIC) [Really: C builtin functions?]

Ken Arnold%CGL arnold at ucsfcgl.UUCP
Fri Apr 11 07:16:03 AEST 1986


In article <202 at chronon.UUCP> Eric Black writes:
>Well, how about sizeof(foo)?
>
>It looks like a function invocation, and is known and understood
>by the compiler...
>
>All right, all right, K & R calls it an "operator", but none of
>us here are known to pick nits, are we??	:-)

Not really.  If "foo" is a variable, you can just as well say "sizeof
foo".  You only need the parentheses if "foo" is a type name, e.g.,
"int".  I almost never use "sizeof (type)" since then I have to know
which type the variable I am associating it with has.  Er, let me try
phrasing that in code.

	some_type *ptr;

	ptr = malloc(10 * sizeof *ptr);

is better (to me) then

	ptr = malloc(10 * sizeof (some_type));

because it is more true.  In the first case I get exactly what I want,
in the second I have no guarantee that the size I'm using relates in
any way to the type of "ptr".  If write the first way, and then change
the type of "ptr" to "some_other_type *ptr", life.

Purists will note that I really should say

	ptr = (some_type *) malloc(10 * sizeof *ptr);

But eventually malloc() will be of type "void *".  This is also an
argument for a new "typeof" operator.  I presume, however, that ANSI
has continued to ignore this, so I guess we're stuck.  I'd be glad to
be disabused of this notion if someone who has access to the latest
draft would tell me.  It sure would be nice to look at it sometime
before it is officially a proposed standard.

		Ken Arnold

P.S.  Another place many people use unecessary parentheses is with
"return".  About 80% of the people I talk with don't even know they
aren't required.



More information about the Comp.lang.c mailing list