我新到C, 遇到一个问题, 例如 K& R C (第1. 9节) 表格不正确 。 以下是我从示例中复制的代码, 有一次我翻过一次, 寻找差异 :
#include <stdio.h>
#define MAXLINE 1000
int mygetline(char line[], int maxline);
void copy(char to[], char from[]);
// print longest input line
main() {
int len;
int max;
char line[MAXLINE];
char longest[MAXLINE];
max = 0;
while ((len = mygetline(line, MAXLINE)) > 0)
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 mygetline(char s[], int lim) {
int c, i;
for (i=0; i<lim-1 && (c=getchar())!=EOF && c!=
; ++i)
s[i] = c;
if (c ==
) {
s[i] = c;
++i;
}
s[i] = ;
return i;
}
// copy: copy from onto to ; assume to is big enough
void copy(char to[], char from[]) {
int i;
i = 0;
while ((to[i] = from[i]) != " ")
++i;
}
当我编集时,我得到以下资料:
cc -Wall -g test.c -o test
test.c:9:1: warning: return type defaults to ‘int’ [-Wreturn-type]
test.c: In function ‘copy’:
test.c:45:30: warning: comparison between pointer and integer [enabled by default]
test.c:45:30: warning: comparison with string literal results in unspecified behavior [-Waddress]
当我管理这个程序时,这种情况会发生:
j) j j
on@j) j jon-G31M-ES2L:~/c$ ./test
Hello, does this work?
Segmentation fault (core dumped)
我用Gcc做我的编译器