Non-Portable pointer assignment?

Tim McDaniel mcdaniel at adi.com
Fri Jun 7 01:13:29 AEST 1991


Followups directed to comp.std.c.

In article <1991Jun5.150527.22942 at klaava.Helsinki.FI>
wirzeniu at klaava.Helsinki.FI (Lars Wirzenius) writes:

   Actually, 'x' is an int. (Being picky, I know.)

It's not "picky"; it can be crucial.  I decided to use
`self-documenting code', as it were, to show the space necessary for
nul bytes, ',' between concatenated strings, and such.

   p = malloc(strlen(s) + sizeof '\0');

It took me a while to figure out that I ought to change it to

   p = malloc(strlen(s) + sizeof (char) '\0');

because sizeof '\0' is 4 on our machines.  Because of FlexeLint (with
a very rare bug), our non-ANSI compilers, and the fact that it's
actually our own MEMmalloc, I actually had to write something like

   p = MEMmalloc((ulong) strlen(s) + (ulong) sizeof ((char) '\0'));

Gak!  I just realized, however, that

   p = malloc(strlen(s) + sizeof "");

should work for ANSI environments.

So why in creation was 'x' ever specified to be int?  Why not char,
with the usual widening in expressions?  And why did X3J11 specify
that the types of enum constants were int, rather than the same type
as the underlying enum type?  It is HIGHLY disconcerting to discover
that a "character constant" is not of type char, and an "enumeration
constant" is not of its enumerated type.

--
   "Of course he has a knife; he always has a knife.  We all have knives.
   It's 1183 and we're barbarians."
Tim McDaniel                 Applied Dynamics Int'l.; Ann Arbor, Michigan, USA
Internet: mcdaniel at adi.com                UUCP: {uunet,sharkey}!amara!mcdaniel



More information about the Comp.std.c mailing list