C 言語コースで K&R を読んでいます。セクション 5.6 の関数 readlines に質問があります。
/*readlines: read input lines*/
int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p, line[MAXLEN];
nlines=0;
while ((len=getline(line, MAXLEN))>0) //getline gets a line of chars and return its
//length. if the length is negative,
// no line exits.
if (nlunes>=maxlines || (p=alloc(len))==NULL) //alloc allocates storage for input
return -1;
else{
line[len-1]='\0' //**THIS IS THE PART I DON'T UNDERSTAND**
strcpy(p,line); //copies line into p (strings)
lineptr(nlines++)=p;
}
return nlines; /*return number of lines we could read*/
したがって、この関数は、qsort とポインターを使用して文字行の配列を辞書順に並べ替える並べ替え関数の一部です。
次の行が何をするのか特にわかりません
line[len-1]='\0' //**THIS IS THE PART I DON'T UNDERSTAND**
「前の行」または「新しい行」を削除する必要があるのはなぜですか?
また、以下もきれいではありません:
p=alloc(len)
ポインターである p にストレージを割り当てることを理解しています。そのため、メモリ内の行の長さに適切なストレージを割り当てます。あれは正しいですか?