Path: utzoo!attcan!uunet!decwrl!shelby!csli!poser From: poser@csli.Stanford.EDU (Bill Poser) Newsgroups: comp.lang.c Subject: Re: Initializing arrays of char Message-ID: <15673@csli.Stanford.EDU> Date: 5 Oct 90 00:53:26 GMT References: <1990Oct4.152756.6850@micrognosis.co.uk> Reply-To: poser@csli.stanford.edu (Bill Poser) Organization: Center for the Study of Language and Information, Stanford U. Lines: 20 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"; I would say that both are erroneous. The reason that you can't assign "12345" to baz is that baz is an array of FIVE chars and the string "12345" requires SIX characters, five for the five digits, and one for the terminating null. The largest string (in the sense of "sequence of characters terminated by a null") that you can put in baz is one four characters long. For this reason, the structure initialization shouldn't work either. Padding of the structure may allocate an additional byte so that the assignment doesn't actually trash anything, but I don't see why the compiler isn't checking the declared array size.