Novice question about malloc and pointers

Steve Summit scs at adam.mit.edu
Fri Apr 19 10:17:39 AEST 1991


In article <3182 at ksr.com> jfw at ksr.com (John F. Woods) writes:
>trotter at 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 at ux1.cso.uiuc.edu> gordon at 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 at adam.mit.edu



More information about the Comp.lang.c mailing list