Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!mcnc!rti!xyzzy!throopw From: throopw@xyzzy.UUCP Newsgroups: comp.lang.c Subject: C arrays of strings Message-ID: <379@xyzzy.UUCP> Date: Wed, 18-Nov-87 11:07:23 EST Article-I.D.: xyzzy.379 Posted: Wed Nov 18 11:07:23 1987 Date-Received: Sat, 21-Nov-87 06:24:43 EST References: <3157@rosevax.Rosemount.COM> Organization: Data General, RTP NC. Lines: 38 > richw@rosevax.Rosemount.COM (Rich Wagenknecht) > Could someone explain the following declaration in english and > give an example of how to use it? "No problem!" --- Alf > What I'm after is simply an array of character strings. > > char *str[12]; Well, what you've got here is an array of twelve pointers to characters. C, of course, has no "character string type" other than by convention, so this may or may not be what you want. In particular, the variable "str" contains space to store twelve addresses, but does *NOT* contain space to store any characters whatsoever. By convention, C stores "character strings" as '\0'-terminated arrays of characters. Further, arrays of characters may often be represented by the address of the first element. Thus, if you have twelve (or fewer) character strings existing independently of the variable "str", and wish to keep track of them, and refer to them by expressions such as (str[n]), then the above declaration is the right one to use. If, on the other hand, you wish to arrange for storage space for some characters organized as twelve strings, you must either allocate the space explicitly if you use the above declaration, or you must declare the space for the characters as part of "str", like so: char str[12][MAX_LEN]; This is an array of twelve arrays of MAX_LEN characters, which can be viewed as an array of twelve strings of at most (MAX_LEN-1) characters. -- "When The Cow MOOS... udders TREMBLE!" --- The Cow, speaking to Madame Marsupial (from The New Adventures of Mighty Mouse) -- Wayne Throop !mcnc!rti!xyzzy!throopw