home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!think.com!ames!agate!stanford.edu!kronos.arc.nasa.gov!butch!netcomsv!netcomsv!ulogic!hartman
- From: hartman@ulogic.UUCP (Richard M. Hartman)
- Newsgroups: comp.lang.c
- Subject: Re: Problem with string processing.
- Message-ID: <846@ulogic.UUCP>
- Date: 11 Jan 93 18:39:38 GMT
- References: <1idn10INNpvg@calamari.hi.com> <1993Jan11.060615.2180@eagercon.com>
- Organization: negligable
- Lines: 46
-
- In article <1993Jan11.060615.2180@eagercon.com> eager@eagercon.com writes:
- >In article 1idn10INNpvg@calamari.hi.com, rogers@calamari.hi.com (Andrew Rogers) writes:
- >>In article <Ts3TwB1w165w@wozzle.linet.org> alane@wozzle.linet.org (J. Alan Eldridge) writes:
- >>>> char tmp[2] = {0};
- >>>>
- >>>> *tmp = ch;
- >>>> strcat(str, tmp);
- >>>
- >>>tmp[0] has a 0 in it but tmp[1] is undefined. You are not creating
- >>>a nul-terminated string here.
- >>
- >>tmp[1] most certainly does have a 0 in it; if the initialization list for
- >>an array contains fewer elements than the array itself, then the remaining
- >>array elements are initialized to 0. Check the ANSI spec, or the relevant
- >>section in K&R.
- >
- >Well, the relevent section is 3.5.7, which says, in part "If an object that
- >has automatic storage duration is not initialized explicitly, its value is
- >indeterminate." Lacking a 'static' declaration and being within a function
- >means that tmp has automatic storage. Only the first element is initialized
- >to zero, tmp[1] is undefined.
- >
- >For static storage objects, all uninitialize elements get binary zero values.
-
- tmp[0] and tmp[1] are NOT separate 'char's, they are a part of an
- array, the initialization of which is governed by it's own set
- of rules.
-
- In K&R (the original, sorry) section 4.9, near the end, regarding
- array initialization it says "If there are fewer initializer than
- the specified size, the others will be zero."
-
- I am certain that this rule has been carried up. I do not have
- the ANSI document, however, to check. The equivilant section for
- the C++ ARM, "8.4.1 Aggregates" where it says "If there are fewer
- initializers in the list than there are members of the aggregate,
- then the aggregate is padded with zeros of the appropriate types."
- I would expect that the ANSI C doc is closer to this than the
- original K&R.
-
- -Richard Hartman
- hartman@uLogic.COM
-
- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- Disco isn't dead... ...it's just in witness protection!
-
-