Char Arrays code in C Programming Book example not working.

huangapple go评论72阅读模式
英文:

Char Arrays code in C Programming Book example not working

问题

我正在研究Brian Kernighan和Dennis Ritchie的《C编程语言第二版》,书中提供的第1.9章中关于字符数组的代码对我不起作用。我确定这不是书的问题,所以肯定是我遗漏了某些东西。但我无法弄清楚是什么。我的代码检查工具没有给我任何错误,但当我尝试编译它时,出现了一堆错误。

错误:

scratch.c:4:5: error: conflicting types for 'getline'
int getline(char line[], int maxline);
    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:365:9: note: previous declaration is here
ssize_t getline(char ** __restrict __linep, size_t * __restrict __linecapp, FILE * __restrict __stream) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
        ^
scratch.c:15:40: error: too few arguments to function call, expected 3, have 2
    while ((len = getline(line, MAXLINE)) > 0) {
                  ~~~~~~~              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:365:9: note: 'getline' declared here
ssize_t getline(char ** __restrict __linep, size_t * __restrict __linecapp, FILE * __restrict __stream) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
        ^
scratch.c:26:5: error: conflicting types for 'getline'
int getline(char s[], int lim) {
    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:365:9: note: previous declaration is here
ssize_t getline(char ** __restrict __linep, size_t * __restrict __linecapp, FILE * __restrict __stream) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);

我的代码

#include <stdio.h>
#define MAXLINE 1000

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

int main() {
    int len; 
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;

    while ((len = getline(line, MAXLINE)) > 0) {
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    }
    if (max > 0)
        printf("%s", longest);
    return 0;
}

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] = '
#include <stdio.h>
#define MAXLINE 1000

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

int main() {
    int len; 
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;

    while ((len = getline(line, MAXLINE)) > 0) {
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    }
    if (max > 0)
        printf("%s", longest);
    return 0;
}

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;
}

void copy(char to[], char from[]) {
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
    ++i;
}
';
return i; } void copy(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != '
#include <stdio.h>
#define MAXLINE 1000

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

int main() {
    int len; 
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;

    while ((len = getline(line, MAXLINE)) > 0) {
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    }
    if (max > 0)
        printf("%s", longest);
    return 0;
}

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;
}

void copy(char to[], char from[]) {
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
    ++i;
}
')
++i; }
英文:

I'm working on Brian Kernighan and Dennis Ritchie's C Programming Language 2nd Edition, and the code provided on 1.9, on character arrays, isn't working for me. I'm sure it's not a problem with the book, so I must be missing something. But I can't figure out what. My linter is not giving me any errors, but when I try to compile it, I get a bunch of errors.

Errors:

scratch.c:4:5: error: conflicting types for &#39;getline&#39;
int getline(char line[], int maxline);
    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:365:9: note: previous declaration is here
ssize_t getline(char ** __restrict __linep, size_t * __restrict __linecapp, FILE * __restrict __stream) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
        ^
scratch.c:15:40: error: too few arguments to function call, expected 3, have 2
    while ((len = getline(line, MAXLINE)) &gt; 0) {
                  ~~~~~~~              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:365:9: note: &#39;getline&#39; declared here
ssize_t getline(char ** __restrict __linep, size_t * __restrict __linecapp, FILE * __restrict __stream) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);
        ^
scratch.c:26:5: error: conflicting types for &#39;getline&#39;
int getline(char s[], int lim) {
    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/stdio.h:365:9: note: previous declaration is here
ssize_t getline(char ** __restrict __linep, size_t * __restrict __linecapp, FILE * __restrict __stream) __OSX_AVAILABLE_STARTING(__MAC_10_7, __IPHONE_4_3);

my code

#include &lt;stdio.h&gt;
#define MAXLINE 1000

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

int main() {
    int len; 
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;

    while ((len = getline(line, MAXLINE)) &gt; 0) {
        if (len &gt; max) {
            max = len;
            copy(longest, line);
        }
    }
    if (max &gt; 0)
        printf(&quot;%s&quot;, longest);
    return 0;
}

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

    for (i=0; i&lt;lim-1 &amp;&amp; (c=getchar()) != EOF &amp;&amp; c != &#39;\n&#39;; ++i) {
        s[i] = c;
    }
    if (c == &#39;\n&#39;) {
        s[i] = c;
        ++i;
    }

    s[i] = &#39;
#include &lt;stdio.h&gt;
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
int main() {
int len; 
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) &gt; 0) {
if (len &gt; max) {
max = len;
copy(longest, line);
}
}
if (max &gt; 0)
printf(&quot;%s&quot;, longest);
return 0;
}
int getline(char s[], int lim) {
int c, i;
for (i=0; i&lt;lim-1 &amp;&amp; (c=getchar()) != EOF &amp;&amp; c != &#39;\n&#39;; ++i) {
s[i] = c;
}
if (c == &#39;\n&#39;) {
s[i] = c;
++i;
}
s[i] = &#39;\0&#39;;
return i;
}
void copy(char to[], char from[]) {
int i;
i = 0;
while ((to[i] = from[i]) != &#39;\0&#39;)
++i;
}
&#39;; return i; } void copy(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != &#39;
#include &lt;stdio.h&gt;
#define MAXLINE 1000
int getline(char line[], int maxline);
void copy(char to[], char from[]);
int main() {
int len; 
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = getline(line, MAXLINE)) &gt; 0) {
if (len &gt; max) {
max = len;
copy(longest, line);
}
}
if (max &gt; 0)
printf(&quot;%s&quot;, longest);
return 0;
}
int getline(char s[], int lim) {
int c, i;
for (i=0; i&lt;lim-1 &amp;&amp; (c=getchar()) != EOF &amp;&amp; c != &#39;\n&#39;; ++i) {
s[i] = c;
}
if (c == &#39;\n&#39;) {
s[i] = c;
++i;
}
s[i] = &#39;\0&#39;;
return i;
}
void copy(char to[], char from[]) {
int i;
i = 0;
while ((to[i] = from[i]) != &#39;\0&#39;)
++i;
}
&#39;) ++i; }

答案1

得分: 1

The problem is that your program uses the identifier getline.

问题在于你的程序使用了标识符 getline

The ISO C standard (any edition) does not define getline, but POSIX does -- and POSIX (a secondary standard) specifies that the getline function is declared in <stdio.h>, one of the standard headers defined by the ISO C standard.

ISO C标准(任何版本)没有定义 getline,但POSIX定义了,而POSIX(次要标准)规定 getline 函数声明在 <stdio.h> 中,这是ISO C标准定义的标准头文件之一。

This is IMHO unfortunate, but it's the way it is.

在我看来,这很不幸,但事实就是如此。

Many C compilers are not conforming by default. gcc and clang (you're likely using the latter) support a number of extensions unless you tell them not to, including functions defined by POSIX.

许多C编译器默认情况下不符合标准。gcc和clang(你可能正在使用后者)支持许多扩展,除非你告诉它们不要支持,包括由POSIX定义的函数。

You can compile with -std=c90 to ask the compiler to conform to the 1990 ISO C standard (the version of the standard used by the book you're using); -std=c89 and -ansi are equivalent. Or you can ask it to conform to a later standard: -std=c99, or -std=c11, or -std-c17. (These options still don't fully conform to the named C standard; you need to add -pedantic for that, to get all required diagnostics.)

你可以使用 -std=c90 编译,要求编译器符合1990年的ISO C标准(你使用的书籍使用的标准版本);-std=c89-ansi 是等效的。或者你可以要求它符合更高版本的标准:-std=c99-std=c11-std-c17。 (这些选项仍然不完全符合所命名的C标准;为此,你需要添加 -pedantic 以获取所有所需的诊断信息。)

The details may differ if you're using a different compiler. C compilers on Unix-like systems tend to try to mimic gcc's behavior, but for example Microsoft's compiler does not. Consult your compiler's documentation if necessary.

如果你使用不同的编译器,细节可能会有所不同。类Unix系统上的C编译器往往会尝试模仿gcc的行为,但例如Microsoft的编译器则不会。如有必要,请查阅你的编译器文档。

Another option is to change your function's name from getline to something else, like get_line, which is not reserved.

另一种选择是将你的函数名称从 getline 更改为其他名称,比如 get_line,这不是保留字。

英文:

The problem is that your program uses the identifier getline.

The ISO C standard (any edition) does not define getline, but POSIX does -- and POSIX (a secondary standard) specifies that the getline function is declared in &lt;stdio.h&gt;, one of the standard headers defined by the ISO C standard.

This is IMHO unfortunate, but it's the way it is.

Many C compilers are not conforming by default. gcc and clang (you're likely using the latter) support a number of extensions unless you tell them not to, including functions defined by POSIX.

You can compile with -std=c90 to ask the compiler to conform to the 1990 ISO C standard (the version of the standard used by the book you're using); -std=c89 and -ansi are equivalent. Or you can ask it to conform to a later standard: -std=c99, or -std=c11, or -std-c17. (These options still don't fully conform to the named C standard; you need to add -pedantic for that, to get all required diagnostics.)

The details may differ if you're using a different compiler. C compilers on Unix-like systems tend to try to mimic gcc's behavior, but for example Microsoft's compiler does not. Consult your compiler's documentation if necessary.

Another option is to change your function's name from getline to something else, like get_line, which is not reserved.

答案2

得分: 0

我一定是漏掉了什么。但我想不出是什么。

正如其他人已经评论的那样,问题在于你所使用的书非常老旧。

在它被发布后,标准的 getline 程序已经被引入,而你(或书中的)的 getline 与标准的冲突。

getline 重命名为例如 my_getline 是最简单的解决方法。

使用更新的书也是另一个可能性。

英文:

> I must be missing something. But I can't figure out what.

As others have commented, the problem is that the book you are following is really old.

After it has been published, a standard getline routine has been introduced, and your (or the book's) getline conflicts with the standard one.

Renaming getline to e.g. my_getline in your source is the easiest solution.

Using a newer book is another possibility.

huangapple
  • 本文由 发表于 2023年3月23日 11:48:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75819099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定