gpt4 book ai didi

尽管未声明,C 编译器仍期望函数中存在附加参数

转载 作者:行者123 更新时间:2023-11-30 14:49:29 24 4
gpt4 key购买 nike

我声明了一个带有 2 个参数的函数,并用 2 个参数调用它,但是编译器似乎将其转换为需要 FILE * __restrict 类型的附加参数的函数。

我使用的是 Mac OSX 10.11.6同时使用 ccgcc 会产生相同的错误。

我应该为函数提供什么参数,或者我应该使用编译器更改设置?或者我只是错过了某个地方的拼写错误......

错误是:

longest-line.c:6:5: error: conflicting types for 'getline'
int getline(char line[], int maxline);
^
/usr/include/stdio.h:442:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
^
longest-line.c:17:37: error: too few arguments to function call, expected 3, have 2
while ((len = getline(line, MAXLINE)) > 0)
~~~~~~~ ^
/usr/include/stdio.h:442:1: note: 'getline' declared here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
^
longest-line.c:28:5: error: conflicting types for 'getline'
int getline(char s[], int lim)
^
/usr/include/stdio.h:442:9: note: previous declaration is here
ssize_t getline(char ** __restrict, size_t * __restrict, FILE * __restrict) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
^
3 errors generated.

代码是:

#include <stdio.h>
#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);

int main()
{
int len; /* current line length */
int max; /* maximum length so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line saved here */

max = 0;
while ((len = getline(line, MAXLINE)) > 0)
if (len > max) {
max = len;
copy(longest, line);
}
if (max > 0) /* there was a line */
printf("%s", longest);

return 0;
}

int getline(char s[], int lim)
{
int c, i;

for (i = 0; i<lim-1 && (c=getchar()) != 'X' && 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;
}

最佳答案

getline 是一个保留名称(在 POSIX 中),您无法顺利使用它,请更改您的函数名称。

关于尽管未声明,C 编译器仍期望函数中存在附加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49584843/

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