Path: utzoo!attcan!uunet!cs.utexas.edu!samsung!uakari.primate.wisc.edu!crdgw1!underdog!volpe From: volpe@underdog.crd.ge.com (Christopher R Volpe) Newsgroups: comp.lang.c Subject: Re: Initializing arrays of char Message-ID: <12459@crdgw1.crd.ge.com> Date: 5 Oct 90 14:03:04 GMT References: <1990Oct4.152756.6850@micrognosis.co.uk> <15674@csli.Stanford.EDU> Sender: news@crdgw1.crd.ge.com Reply-To: volpe@underdog.crd.ge.com (Christopher R Volpe) Lines: 37 In article <15674@csli.Stanford.EDU>, poser@csli.Stanford.EDU (Bill Poser) writes: |>Regarding the assignment of "12345" to char x[5] and struct{char x[5]}, |>I spoke too soon. K&R2 contains a detail I hadn't noticed, and am not |>sure that I approve of. On p.219, in the discussion of initialization |>of fixed size arrays by string constants, it states: |> |> ...the number of characters in the string, NOT COUNTING |> THE TERMINATING NULL CHARACTER, must not exceed the |> size of the array. [emphasis mine] |> |>This means that the assignment of "12345" to an array of five characters, |>is legal. If K&R2 here reflects the standard, then both initializations |>are legitimate. |> |>This seems to me to be a bad idea. Everywhere else, one has to take |>into account the terminating null. For example, x[5] = 'a' is |>an error. Not counting the terminating null here is inconsistent. |>Can anyone explain this decision? The fact that x[5] = 'a' is an error has nothing to do with any terminating null. It's an error because x[5] doesn't exist. The array has elements x[0] through x[4]. There may be situations where you just want an array of characters, and DON'T want a "string" (null terminated). Thus, you have the capability of creating a five-byte array of char and initializing it with "abcde" and a six-byte string and initializing it with "abcde" also. If you don't like having to remember to allocate space for the terminating null when declaring the array, let the compiler do it for you: char x[] = "abcde"; will create an array of six chars and initialize it, including the terminating null. ================== Chris Volpe G.E. Corporate R&D volpecr@crd.ge.com