Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!bloom-beacon!oberon!cit-vax!elroy!mahendo!jplgodo!wlbr!voder!pyramid!fmsrl7!mibte!cfpas!jhritz From: jhritz@cfpas.UUCP (John Hritz) Newsgroups: comp.lang.c Subject: Re: C arrays of strings Message-ID: <231@cfpas.UUCP> Date: Wed, 18-Nov-87 14:54:05 EST Article-I.D.: cfpas.231 Posted: Wed Nov 18 14:54:05 1987 Date-Received: Sat, 21-Nov-87 12:16:45 EST References: <3157@rosevax.Rosemount.COM> Reply-To: jhritz@cfpas.UUCP (John Hritz) Organization: Chrysler FPAS, Southfield, MI Lines: 66 Keywords: C In article <3157@rosevax.Rosemount.COM> richw@rosevax.UUCP writes: >Could someone explain the following declaration in english and >give an example of how to use it? I believe I can use this declaration >but cannot explain how is works. What I'm after is simply an array of >character strings. > > char *str[12]; Although it is not fool proof, a useful method for converting C declarations to English is to use the outside/inside rule, using the type and variable names as your boundaries. Using your example, char *str[12] would be translated to "str is an array of 12 pointers to characters". Using one of favorite examples in K and R, this construct could be used to store a list of months. Each month is variable length, you have only a pointer to where the characters are stored. Like so, static char *str[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; Note that in the absence of a declared size of the array, the compiler will calculate the number of elements, in this case 12. This contrasts with declaring the length of each of the elements which looks similar. Ex. static char str[][80] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; This will calculate the number of initializers but then allocate 80 characters for each line. Its not useful in this example, but its often done to initialize strings with default values and allow the user of the application to alter their contents with entries up to 80 characters long. So, to create an array of strings, the declaration would be something like char str[10][80] or in english "an array of 10 strings each 80 characters in length. For more information on creating and reading C type declarations refer to The C Programming Language, Kerninghan and Ritchie, pp 194-195. -- UUCP: ihnp4!mibte!cfpas!jhritz (John Hritz) "Do photons have mass?... VOICE: 313-351-3485 Are any of them Catholic?"