问题遇到的现象和发生背景
在一本c语言指导书里发现一个使用指针数组读取输入字符串并进行排序的程序代码,发现有点没看懂,麻烦各位帮我看看41行出的gets(buffer)!=0是什么意思,这不是相当于指针为零吗?还有45行return -1是什么意思,这个值返回到哪里,是EOF的作用吗?
用代码块功能插入代码,请勿粘贴截图
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 25
int get_lines(char *lines[]);
void sort(char *p[],int n);
void print_strings(char *p[],int n);
char *lines[max];
int main()
{
int num_of_lines;
num_of_lines=get_lines(lines);
if(num_of_lines<0)
{
puts("memory allocation error.");
exit(-1);
}
sort(lines,num_of_lines);
print_strings(lines,num_of_lines);
return 0;
}
int get_lines(char *lines[])
{
int n=0;
char buffer[80];
puts("enter one line at time,enter a blank when done.");
while((n<max)&&(gets(buffer)!='\0')&&(buffer[0]!='\0'))
{
if((lines[n]=(char*)malloc(strlen(buffer)+1))==NULL)
{
return -1;
}
strcpy(lines[n++],buffer);
}
return n;
}
void sort(char *p[],int n)
{
int a,b;
char *tmp;
for(a=1;a<n;a++)
{
for(b=0;b<n-1;b++)
{
if(strcmp(p[b],p[b+1])>0)
{
tmp=p[b];
p[b]=p[b+1];
p[b+1]=tmp;
}
}
}
}
void print_strings(char *p[],int n)
{
int count;
for(count=0;count<n;count++)
{
printf("%s\n",p[count]);
}
}