Newsgroups: comp.lang.c Path: utzoo!henry From: henry@zoo.toronto.edu (Henry Spencer) Subject: Re: Initializing arrays of char Message-ID: <1990Oct5.171521.27673@zoo.toronto.edu> Organization: U of Toronto Zoology References: <1990Oct4.152756.6850@micrognosis.co.uk> Date: Fri, 5 Oct 90 17:15:21 GMT 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"}; It's a compiler bug. ANSI C, 3.5.7 (emphasis added): An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character *if there is room* or if the array is of unknown size) initialize the elements of the array. Your compilers are assuming that "12345" has six characters in it, which is correct in general, but for this oddball special case in initializers the terminating null is present only if there is room for it. -- Imagine life with OS/360 the standard | Henry Spencer at U of Toronto Zoology operating system. Now think about X. | henry@zoo.toronto.edu utzoo!henry