Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!lll-winken!csustan!polyslo!sdejarne From: sdejarne@polyslo.UUCP (Steve DeJarnett) Newsgroups: comp.lang.c Subject: Re: C arrays of strings Message-ID: <730@polyslo.UUCP> Date: Tue, 24-Nov-87 15:26:46 EST Article-I.D.: polyslo.730 Posted: Tue Nov 24 15:26:46 1987 Date-Received: Sat, 28-Nov-87 00:49:38 EST References: <3157@rosevax.Rosemount.COM> Reply-To: sdejarne@polyslo.UUCP (Steve DeJarnett) Organization: Cal Poly State Univ,CSC Dept,San Luis Obispo,CA 93407 Lines: 54 Keywords: C Summary: I hope I get this right! In article <3157@rosevax.Rosemount.COM> richw@rosevax.Rosemount.COM (Rich Wagenknecht) 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]; This declaration gives you an array (12 members) of pointers-to-char. There is no storage reserved for the character strings, only 32-bits (in most cases) to hold the address of a character string. This declaration is just like the char *argv[] that you get from the command line of your program. To get space for the character strings, you would need to do something like: str[1] = (char *) malloc(100); <--- gets you space for 100 chars. Note: both sides of the = sign have to represent the same thing (i.e. you don't want to go assigning an integer to a character (usually :-)). To make sure you have the right thing on both sides, cover up the part in the declaration that you have in your equation: In this case cover up the str[12] in the declaration and the str[1] in the equation. Whatever is left over on the declaration line is what you have (in this case, a char pointer). I hope that made sense, but if not, ignore it. Here is some code that uses char *str[12]. void func() { char *str[12]; char *malloc(); int i; for (i=0;i<=12;i++) { str[i] = malloc(80); printf("enter a string> "); gets(str[i]); } for (i=0;i<=12;i++) printf(String %d = %s\n",i,str[i]); return; } Hope this helps. ------------------------------------------------------------------------------- | Steve DeJarnett | ...!ihnp4!csun!polyslo!sdejarne | | Computer Systems Lab | ...!{csustan,csun,sdsu}!polyslo!sdejarne | | Cal Poly State Univ | ...!ucbvax!voder!polyslo!sdejarne | | San Luis Obispo, CA 93407 | | ------------------------------------------------------------------------------- #include