我正在用K&R书学习C语言.有一个练习,这里是:"编写一个程序来打印超过80个字符的所有输入行".所以我写了这段代码:
#include <stdio.h>
#include <stdlib.h>
int getline(char s[], int lim);
#define MINLINE 80
#define MAXLINE 1000
/*
*
*/
int main(int argc, char** argv) {
int len; //current line length
char line[MAXLINE]; //current input line
int proceed=0;
while((len=getline(line, MAXLINE))>0)
if(line[len-1]!='\n'){
printf("%s", line);
proceed=1;}
else if(proceed==1){
printf("%s", line);
proceed=0;}
else if(len>MINLINE){
printf("%s", line);
}
return 0;
}
int getline(char s[], int lim){
int i, c;
for(i=0; i<lim-1 && (c=getline())!='*' && c!='\n'; i++){
s[i]=c;
}
if(c=='\n'){
if(i<=lim-1){
s[i]=c;}
i++;}
s[i]='\0';
return i;
}
Run Code Online (Sandbox Code Playgroud)
我无法编译它,我不知道如何解决它.你可以帮帮我吗?
这是错误消息:
main.c:11:5: error: conflicting types for ‘getline’
In file included from /usr/include/stdio.h:62:0,
from main.c:8:
/usr/include/sys/stdio.h:37:9: note: previous declaration of ‘getline’ was here
main.c:38:5: error: conflicting types for ‘getline’
In file included from /usr/include/stdio.h:62:0,
from main.c:8:
/usr/include/sys/stdio.h:37:9: note: previous declaration of ‘getline’ was here
main.c: In function ‘getline’:
main.c:40:5: error: too few arguments to function ‘getline’
main.c:38:5: note: declared here
make[2]: *** [build/Debug/Cygwin_4.x_1-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
Run Code Online (Sandbox Code Playgroud)
getline()函数已在stdio.h头文件中声明.如果要在文件中重新定义它.只需修改为 my_getline()
在这个for循环中,你需要使用getchar()不getline()
for(i=0; i<lim-1 && (c=getline())!='*' && c!='\n'; i++)
for(i=0; i<lim-1 && (c=getchar())!='*' && c!='\n'; i++)
Run Code Online (Sandbox Code Playgroud)
你需要在你的函数中使用指针来使输入成行.其他方面是函数的本地.
int my_getline(char *, int); //declaration
int my_getline(char *s, int lim) //defination
{
//....
}
Run Code Online (Sandbox Code Playgroud)
函数调用是一样的
len= my_getline(line, MAXLINE)
Run Code Online (Sandbox Code Playgroud)
最后使用一些条件机制来摆脱main中的while循环.
| 归档时间: |
|
| 查看次数: |
1050 次 |
| 最近记录: |