Extension to solution given to a novice

csp at gtenmc.UUCP csp at gtenmc.UUCP
Tue Apr 30 02:38:20 AEST 1991


>  This proves without doubt 'Ignorance is NOT bliss'.

followed by:
>  str[i] = malloc(strlen(gets(buff)+1)),

   The points you have raised are valid, but IMHO giving a routine which
   handles input with rigor, would even confuse the novice even further.
   But if you insist here is a routine which will handle the input will
   some rigor. 

      Hope it satisfies your urge towards perfection.


#include <stdio.h>
#include <malloc.h>

#define MEM_ERR 1

extern err_no;

char *mygets();

char *mygets(l,t)
unsigned l;
char t;   /* String terminator */
{
   char *ptr,c;
   c = getchar();
   if (( int )c != -1 && c != t )
      *( ptr = mygets( l + 1 , t )) = c;
   else
   {
      if (( ptr = malloc( l + 1 )) == ( char * ) NULL )
	 return( err_no = MEM_ERR , ( char * ) NULL );
      *( ptr += l ) = 0;
   }
   return( ptr - 1 * ( l > 0 ));
}

C S Palkar



More information about the Comp.lang.c mailing list