The following function readline() is used to read the lines from console and store the pointers to each line into the *lineptr[MAXLINES].
Code:
#define MAXLINES 5000
#define MAXLEN 1000
int getline(char *line, int maxlen);
int readlines(char *lineptr[], int maxlines);
char *lineptr[MAXLINES];
int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p, line[MAXLEN];
nlines=0;
while((len=getline(line, MAXLEN)>0)
if(nlines>=maxlines ||(p=(char *)malloc(len))==NULL)
return -1;
else
{
lines[len-1]='\0';/*delete newline*/
strcpy(p, line);
lineptr[nlines++]=p;
}
return nlines;
}
How can we write this function using only the *lineptr[] but not using the malloc to maintain storage?