Path: utzoo!attcan!uunet!cs.utexas.edu!samsung!noose.ecn.purdue.edu!mentor.cc.purdue.edu!edgincd2 From: edgincd2@mentor.cc.purdue.edu (Chris Edgington *Computer Science Major*) Newsgroups: comp.lang.c Subject: Re: Initializing arrays of char Summary: You need to account for the NULL Message-ID: <14796@mentor.cc.purdue.edu> Date: 5 Oct 90 01:14:08 GMT References: <1990Oct4.152756.6850@micrognosis.co.uk> Organization: Purdue University Lines: 28 In article <1990Oct4.152756.6850@micrognosis.co.uk>, nreadwin@micrognosis.co.uk (Neil Readwin) writes: > > Can someone tell me why the following initializer is legal inside a > structure, but not outside it ? Or is it a compiler bug ? > > struct foo { > char x[5]; > } bar = {"12345"}; > > char baz[5] = "12345"; > When you request the compiler to allocate n chars in the array, you really should only use the first 4 if you are going to be using the array as a string because one of the chars allocated is used for the NULL, which tells the compiler where the end of the string is. If you write over the NULL and then try to print the string, the compiler [runtime code] will just continue printing until it encounters a NULL, signifying the end of the string. Therefore, to allocate ample space for your string "12345", you need to have char baz[6]. __ __ / ) / / ` / _/_ / /_ __ o _ /-- __/ _, o ____ _, / ________ (__/ / /_/ (_<_/_)_ (___, (_/_(_)_<_/ / <_(_)_<__(_) / / <_ /| /| |/ |/ Chris Edgington edgincd2@mentor.cc.purdue.edu Purdue University