[英]Conflicting types with a declared int func
读取Kernigan时,由于用户功能出现“冲突类型”错误,给定的代码似乎不起作用。
我使用的编译器是cc 7.4.0。 我在一开始就声明了该函数,并且似乎一直在检查和重新检查类型。 猜猜我缺少什么。
#include <stdio.h>
#define MAXLINE 1000
int getline(char line[], int maxline);
main()
{
int len;
char line[MAXLINE];
while((len = getline(line, MAXLINE)) > 0)
;
return 0;
}
int getline(char s[], int lim)
{
int i, c;
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;
}
我得到的错误是
func.c:4:5: error: conflicting types for ‘getline’
int getline(char line[], int maxline);
^~~~~~~
In file included from func.c:1:0:
/usr/include/stdio.h:616:20: note: previous declaration of ‘getline’ was here
extern _IO_ssize_t getline (char **__restrict __lineptr,
^~~~~~~
^~~~
func.c:25:5: error: conflicting types for ‘getline’
int getline(char s[], int lim)
^~~~~~~
In file included from func.c:1:0:
/usr/include/stdio.h:616:20: note: previous declaration of ‘getline’ was here
extern _IO_ssize_t getline (char **__restrict __lineptr,
getline
已在<stdio.h>
声明(实际上是错误的),并且其签名与您的签名不同。
最简单的操作是将您的版本重命名为其他名称。
您需要阻止编译器将非标准废话涌入标准标头。 您正在使用某个编译器,该编译器在标准库头文件stdio.h
定义了一个非标准的getline
函数,该函数不符合标准。
使用gcc时,您应该像gcc -std=c17 -pedantic-errors
进行编译,以防止发生这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.