c新手,並試圖編寫我的第一個程序,該程序將從每行文本的右側修剪空白處並返回它。編譯當我得到:編寫一個程序從C文本的右側修剪空白空間C
trim.c: In function âmainâ:
trim.c:15:2: warning: passing argument 1 of âgetlineâ from incompatible pointer type [enabled by default] /usr/include/stdio.h:675:20: note: expected âchar ** __restrict__â but argument is of type âchar *â
trim.c:15:2: warning: passing argument 2 of âgetlineâ makes pointer from integer without a cast [enabled by default] /usr/include/stdio.h:675:20: note: expected âsize_t * __restrict__â but argument is of type âintâ
trim.c:15:2: error: too few arguments to function âgetlineâ /usr/include/stdio.h:675:20: note: declared here
trim.c:15:41: error: expected expression before â:â token
trim.c:21:27: error: expected â)â before â;â token
trim.c:22:1: warning: passing argument 1 of âprintfâ makes pointer from integer without a cast [enabled by default] /usr/include/stdio.h:363:12: note: expected âconst char * __restrict__â but argument is of type âintâ
trim.c:22:1: warning: format not a string literal and no format arguments [-Wformat-security]
trim.c:22:1: error: expected â;â before â}â token trim.c: At top level: trim.c:26:6: error: conflicting types for âgetlineâ /usr/include/stdio.h:675:20: note: previous declaration of âgetlineâ was here trim.c: In function âgetlineâ:
我的代碼是:
#include <stdio.h>
#define MAXLINE 1000
char line[MAXLINE];
int len;
main()
{
int i;
int j;
while ((len = getline(line, MAXLINE)) > 0)
{
for (i = len; line[i] < 31; --i)
line[i] = 0;
line[i + 1] = 10;
for (j = 0; j <= i; j++)
printf(putchar(line[j]));
}
}
int getline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) !EOF && c != '\n'; ++i)
s[i] = c;
len = i;
return i;
}
我在正確的軌道上?我相信編譯器引用的第一個問題與我傳遞給getline()函數的類型有關。任何信息或建議將不勝感激。
「'的printf(的putchar(線[J]);'」 - 你還沒有關閉括號從'printf()',也請把你的錯誤放在代碼塊中,不要去掉換行符 –
得到一些教程,給你一個程序的總體佈局,例如原型,返回值等。 –
解釋第15行正在做什麼 – smac89