繁体   English   中英

C编程语言1.9中示例的冲突类型

[英]conflicting types for the sample from The C Programming Language 1.9

我是C语言的新手,我使用的是C编程语言,我抄下了1.9,试图打印最长的一行。 当我尝试在Xcode中构建程序时,它表明存在类型冲突和表达式问题的错误,但我尝试将函数“ getline”放在main之前,但我仍然不知道出了什么问题。

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */
int getline(char line[ ], int marline);     ----ISSUE:Conflicting types for "getline"
void copy(char to[ ], char from[ ]);

/* print longest input line*/
int main(){
int len; /* current line length */
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)     ------ISSUE:Expected Expression
    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){            ----ISSUE:Conflicting types for "getline"    
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;}

它与标准库的getline()函数冲突,该函数在编写该书时不存在(它来自POSIX.1-2008,该书年龄较大)。

在示例中重命名该函数,以避免冲突。 只需使用mygetline()代替,就可以了。

在ISO C中,您的程序正常,但是您使用的是默认为POSIX C的编译器。

如果使用支持ISO兼容模式的编译器,请通过编译器开关调用该模式。 对于gcc是-std=c11-std=c99

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM