Newsgroups: comp.lang.c Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!m.cs.uiuc.edu!ux1.cso.uiuc.edu!osiris.cso.uiuc.edu!gordon From: gordon@osiris.cso.uiuc.edu (John Gordon) Subject: Re: Novice question about malloc and pointers Message-ID: <1991Apr17.203253.13854@ux1.cso.uiuc.edu> Sender: usenet@ux1.cso.uiuc.edu (News) Organization: University of Illinois at Urbana References: <9104171614.AA14362@enuxha.eas.asu.edu> Date: Wed, 17 Apr 1991 20:32:53 GMT Lines: 20 trotter@ENUXHA.EAS.ASU.EDU (Russell T. Trotter) writes: >I am trying to get an array of strings, therefore I am using the >following declaration: char *str[MAX] where MAX is an arbitrary >constant. My question is how do I allocate the memory for each >character position? Do all the characters strings for each element >in the array need to be allocated contiguously? The problem involves >reading in lines of input. Each line would be stored as a string and >the number of lines make up the number of elements in the array. >Any information would be greatly appreciated. Thank you. Here's how I did something similar: use a temporary variable such as char buf[200] for getting each line of input, then do a str[i] = (char *) malloc(strlen(buf) +1) to copy each line into str. --- John Gordon Internet: gordon@osiris.cso.uiuc.edu #include gordon@cerl.cecer.army.mil #include