求教为什么输入一行字符串后程序就被终止
这是c primer plus上面的例题,并未发现和书上有什么差异,为什么输入一行后就被终止了?
程序代码:/*输入几行字符串,并将其从小到大排序*/
#include <stdio.h>
#include <string.h>
#define SIZE 81//字符串长度限制,包括\0
#define LIM 20//最多读取的行数
void stsrt (char * strings[], int num);//字符串排序函数
int main (void)
{
char input[LIM][SIZE];//存储输入的数组
char * p[LIM];//指针变量的数组
int ct;//输入计数
int k;//输出计数
printf("Input up to %d lines, and I will sort them.\n", LIM);
printf("To stop, press the Enter key at a line's start.\n");
while(ct < LIM && gets(input[ct]) != NULL && input[ct][0] != '\0')
{
p[ct] = input[ct];//指针指向输入字符串
ct++;
}
stsrt(p, ct);//调用字符串排序函数
puts("\nHere's the sorted list:\n");
for(k = 0; k < ct; k++)
puts(p[k]);
return 0;
}
void stsrt (char * strings[], int num)
{
char * temp;
int top,seek;
for(top = 0; top < num -1; top++)
for(seek = top + 1; seek < num; seek++)
if(strcmp(strings[top], strings[seek]) > 0)
{
temp = strings[top];
strings[top] = strings[seek];
strings[seek] = temp;
}
}







