Mik*_*keG 2 c kernighan-and-ritchie
有人可以看看这个并告诉我有什么问题.我有3个错误.1)error: Conflicting types for getline2)error: too few arguments to function call, expected 3 have 23)error: conflicting types for getline.我确定我忽略了一些简单但我无法找到错误的东西.谢谢,这是代码......
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline); /*conflicting types for getline*/
void copy(char to[], char from[]);
main(){
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while((len = getline(line, MAXLINE)) > 0)/*too few arguments to call, expected 3 have 2*/
if (len > max){
max = len;
copy(longest, line);
}
if (max > 0)
//printf("longest line = %d characters\n -->", max);
printf("%s", longest);
return 0;
}
int getline(char s[], int lim){/*conflicting types for getline*/
int c, i;
for(i = 0; i<lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
void copy(char to[], char from[]){
int i;
i = 0;
while((to[i] = from[i]) != '\0'){
++i;
}
}
Run Code Online (Sandbox Code Playgroud)
GNU 函数getline具有相同的功能,但它不是 C 标准的一部分。据推测,您正在编译时没有-std指定(特定的 C 标准),这使得从<stdio.h>.
您可以使用-std=c99以下命令进行编译-std=c11:
gcc -Wall -Wextra -std=c11 gl.c
Run Code Online (Sandbox Code Playgroud)
或者将您的函数重命名为类似的名称my_getline()以避免这种冲突。
另外,main()的签名必须是标准签名之一。由于您不使用命令行参数,因此可以使用:
int main(void) { .. }
Run Code Online (Sandbox Code Playgroud)
请参阅:在 C 和 C++ 中 main() 应该返回什么?
答案很简单:不要打电话给你的功能getline.这个名字已被采用.
(我知道,这是K&R的一个例子.所以你应该能够使用它,对吧?不幸的是,没有.也是我的错误.)
稍微长一点的答案:现在有一个半标准getline功能,与你的冲突.如果您将名称与自己的某个功能联系在一起,那么您将获得同样的错误printf.可能有一种方式可以说,"我不想使用标准getline函数,我想使用自己的函数",但在这种情况下,它可能不值得.
(就个人而言:我已经用C语言编程了大约35年,getline从那时起我已经使用了自己的功能大约34.9年,自从我在K&R上读到它以来.但是在过去的一年左右我一直不得不重写我的所有程序,fgetline而不是调用我自己的函数来getline解决这个问题.)
| 归档时间: |
|
| 查看次数: |
582 次 |
| 最近记录: |