Path: utzoo!attcan!uunet!bu.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!ucsd!pacbell.com!ames!dftsrv!jagmac2.gsfc.nasa.gov!jim From: jim@jagmac2.gsfc.nasa.gov (Jim Jagielski) Newsgroups: comp.lang.c Subject: Re: Initializing arrays of char Message-ID: <3568@dftsrv.gsfc.nasa.gov> Date: 5 Oct 90 16:45:13 GMT References: <1990Oct4.152756.6850@micrognosis.co.uk> <14796@mentor.cc.purdue.edu> <9418:Oct503:06:2790@kramden.acf.nyu.edu> Sender: news@dftsrv.gsfc.nasa.gov Reply-To: jim@jagmac2.gsfc.nasa.gov (Jim Jagielski) Organization: NASA/Goddard Space Flight Center Lines: 39 In article <9418:Oct503:06:2790@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes: >In article <14796@mentor.cc.purdue.edu> edgincd2@mentor.cc.purdue.edu (Chris Edgington *Computer Science Major*) writes: >> In article <1990Oct4.152756.6850@micrognosis.co.uk>, nreadwin@micrognosis.co.uk (Neil Readwin) writes: >> > char baz[5] = "12345"; > [ explanation ] > >> Therefore, to allocate ample space for your string "12345", you need to have >> char baz[6]. > >Only if you really do mean it that way---but from your article you >obviously know how many characters to allocate for a NULL-terminated >string, so you wouldn't be asking if that were the answer. (For those >new to C, the easy way to allocate a string is char baz[] = "12345";.) > -------------------- ^ Let's clarify the above ---------------------------| You forget that doing "char baz[] = "12345";" is the same as: char baz[] = { '1','2','3','4','5','\0' }; It appears in the above that you imply that doing char baz[] = "12345"; would result in a non-NULL-terminated string -- this is not correct. Of course, I may not be reading your mind right :) In any case, recall that C will always append the '\0' to any string constant. If you don't want \0 in there, either copy upto the NULL (strncpy) or use characters ('a', etc...) -- ======================================================================= #include =:^) Jim Jagielski NASA/GSFC, Code 711.1 jim@jagmac2.gsfc.nasa.gov Greenbelt, MD 20771 "Kilimanjaro is a pretty tricky climb. Most of it's up, until you reach the very, very top, and then it tends to slope away rather sharply."