Novice question about malloc and pointers

csp at gtenmc.UUCP csp at gtenmc.UUCP
Sat Apr 27 04:56:05 AEST 1991


In article <9104171614.AA14362 at enuxha.eas.asu.edu> trotter at enuxha.eas.asu.edu (Russell T. Trotter) writes:
>constant.  My question is how do I allocate the memory for each 
                                    ^^^^^^^^
	    Malloc , my dear Russell. Malloc !
>character position? Do all the characters strings for each element
>in the array need to be allocated contiguously?  The problem involves
                                   ^^^^^^^^^^^^
	    Did't you know that memory is linear ! ( NO )
>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.

	    This proves without doubt 'Ignorance is NOT bliss'.

	 
	 Solution :
	 #include <string.h>
	 #include <malloc.h>
	 #include <stdio.h>

	 #define MAX 20
	 #define MAX_SZ 512

	 char *str[MAX];

	 Read_Input()
	 {
	    int i;
	    char buff[MAX_SZ];

	    for ( i = 0 ; i < MAX ; i++ )
	       str[i] = malloc(strlen(gets(buff)+1)),
	       strcpy(str[i],buff);
	 }

	 C S Palkar
	 --
>
>trotter at enuxha.eas.asu.edu



More information about the Comp.lang.c mailing list