簡體   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