2016-10-11 45 views
0

我是C新手,我使用的是C編程語言,我抄下1.9試圖打印最長的一行。雖然我嘗試在Xcode中構建程序,但它顯示出存在類型衝突和表達問題的錯誤,但我仍試圖將函數「getline」放在main之前,但我仍然不知道發生了什麼問題。The C Programming Language 1.9

#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;} 
+2

請正確格式化您的代碼。 –

+0

[getline](http://man7.org/linux/man-pages/man3/getdelim.3.html)是保留函數。 ((len = getline(line,MAXLINE))> 0)'重命名該函數,例如:'my_getline' – LPs

+0

'while((len = getline(line [],MAXLINE))> 0)' - >'。其餘的,請參閱下面的答案。 –

回答

1

這是相互矛盾的標準圖書館的的getline()功能,當那本書是寫並不存在(這是由POSIX.1-2008,這本書是...以上)。

重命名示例中的函數,以避免碰撞。只需使用mygetline(),您應該沒問題。

1

在ISO C中,你的程序是可以的,但是你使用的是一個默認爲POSIX C的編譯器,或者其他東西。

如果您使用的編譯器支持ISO一致性模式,請通過編譯器開關調用該模式。對於海灣合作委員會,它是-std=c11-std=c99