gpt4 book ai didi

与声明的 int 函数冲突的类型

转载 作者:太空宇宙 更新时间:2023-11-04 05:32:21 26 4
gpt4 key购买 nike

阅读 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> 中声明(实际上是错误的)并且它的签名与你的不同。

最简单的做法是将您的版本重命名为不同的名称。

关于与声明的 int 函数冲突的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57788856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com