K&安培; R第2版,例1.9字符数组 [英] K&R 2nd Edition, Example 1.9 Character Arrays
问题描述
我有在以下code问候则对getline()函数和参数的定义的问题。在code是直接从K&放取; R 1.9章:字符数组。我已复制在这里一字不差。问题是,当我编译程序的是我得到三个错误,(我在年底纷纷转载)。当我在三个地方我得到的错误更改功能,和功能参数定义get_line()(带有下划线,而不是仅仅函数getline),错误停止,程序运行正常。
I have a question in regards to the getline() function and parameter definition in the following code. The code is taken directly from K&R chapter 1.9: "Character arrays". I have reproduced it here verbatim. The issue is that when I compile the program as is I get three errors,(which I have reproduced at the end). When I change the function, and the function parameter definition to get_line() (with an underscore instead of just getline) in the three places where I get the errors, the errors stop and the program runs as expected.
我的问题是:
#include <stdio.h>
#define MAXLINE 1000 // maximum input line size
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print longest input line */
int main()
{
int len; //current line lenght
int max; //maximum length seen 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;
}
/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
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;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[],char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\0') {
++i;
}
}
我得到的错误是:
The errors I get are:
-
./部分1.9.1.c:4:5:错误:冲突的类型'函数getline';
INT函数getline(INT线[],INT MAXLINE);
./部分1.9.1.c:17:40:错误:太少参数函数调用,预计3,有2个,而((LEN =函数getline(线,MAXLINE))0);
和
./section 1.9.1.c:17:40: error: too few arguments to function call, expected 3, have 2
while ((len = getline(line, MAXLINE)) > 0);
and
./部分1.9.1.c:30:5:错误:冲突的类型'函数getline INT函数getline(int类型[],INT LIM)
./section 1.9.1.c:30:5: error: conflicting types for 'getline'
int getline(int s[], int lim)
推荐答案
与的glibc
分布式stdio库声明一个是也被称为函数函数getline
有比你不同的签名。既然你不能使用相同的名称声明两个函数编译器提供了一个错误。 函数getline
的相互矛盾的声明在stdio.h发现的是:
The stdio library distributed with glibc
declares a function that is also called getline
with a different signature than yours. Since you cannot declare two functions with the same name the compiler gives an error. The conflicting declaration of getline
found in stdio.h is:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
的函数getline
功能原本是glibc的扩展,并且再被列入POSIX.1-2008。 这是不是一个标准的C函数。
The getline
function was originally a glibc extension, and has then been included in POSIX.1-2008. It is not a standard C function.
如果您使用的是 GCC
您可以通过使用 -std
命令行开关获得符合标准的行为。别的不说这个隐藏非标准函数的声明。尝试例如:
If you are using gcc
you can get standards-compliant behavior by using the -std
command line switch. Among other things this hides declarations of non-standard functions. Try for example:
gcc -Wall -pedantic -std=c11 "section 1.9.1.c" -o "section 1.9.1"
这篇关于K&安培; R第2版,例1.9字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!