Newsgroups: comp.lang.c Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-picayune.mit.edu!news From: scs@adam.mit.edu (Steve Summit) Subject: Re: Novice question about malloc and pointers Message-ID: <1991Apr19.001739.5485@athena.mit.edu> Summary: two nits Sender: news@athena.mit.edu (News system) Reply-To: scs@adam.mit.edu Organization: Thermal Technologies, Inc. References: <9104171614.AA14362@enuxha.eas.asu.edu> <3182@ksr.com> <1991Apr17.203253.13854@ux1.cso.uiuc.edu> Date: Fri, 19 Apr 91 00:17:39 GMT Lines: 21 In article <3182@ksr.com> jfw@ksr.com (John F. Woods) writes: >trotter@ENUXHA.EAS.ASU.EDU (Russell T. Trotter) writes: >>I am trying to get an array of strings... >>...how do I allocate the memory for each [string]? >The most obvious implementation... is: [most code deleted] > /* Treasure the line in a copy */ > if ((str[i] = malloc(strlen(buf))) == NULL) { Make that malloc(strlen(buf) + 1). (Side note: never tangle your fingers and type malloc(strlen(buf + 1)), either.) In article <1991Apr17.203253.13854@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes: >...do a >str[i] = (char *) malloc(strlen(buf) +1) >to copy each line into str. make that "do [a malloc], *then* copy each line into str." Steve Summit scs@adam.mit.edu