From page 29 K&R 2nd 1988, So here it goes (With some mods from me):
insert
Code:
#include <stdio.h>
main() {
int len;
int max =0;
char line[500];
char longest[500];
while( (len = getline(line, 500)) > 0) {
if( len > max ){
max = len;
copy(longest, line);
}
}
if (max > 0)
printf("%s", longest);
// return 0;
}
int getline(char that_line[], int lim){
int c, i;
for (i=0; i < lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
that_line[i] = c;
if (c == '\n') {
that_line[i] = c;
++i;
}
that_line[i] = '\0';
return i;
}
void copy(char dest[], char from[]) {
int i = 0;
while( (dest[i] = from[i]) != '\0')
++i;
}
$cc example.c
Here the the error msgs:
19.c: In function ‘main’:
19.c:10: warning: passing argument 1 of ‘getline’ from incompatible pointer type
/usr/include/stdio.h:671: note: expected ‘char ** __restrict__’ but argument is of type ‘char *’
19.c:10: warning: passing argument 2 of ‘getline’ makes pointer from integer without a cast
/usr/include/stdio.h:671: note: expected ‘size_t * __restrict__’ but argument is of type ‘int’
19.c:10: error: too few arguments to function ‘getline’
19.c: At top level:
19.c:21: error: conflicting types for ‘getline’
/usr/include/stdio.h:671: note: previous declaration of ‘getline’ was here
19.c:36: warning: conflicting types for ‘copy’
19.c:13: note: previous implicit declaration of ‘copy’ was here
So whats wrong with the code..
Has the standard changed much since to C99 or something that the compiler uses..
Yeah the code was modded bit by me...
but I doubt if I wrote it carbon copy- or use Visual Studio the result will differ ?
Like many ppl today, I guess I m from Java..
Can someone explain why it failed & what has changed maybe since..
What's a good reference book for learning the language.
Assume I know quite some about Java can program well already..
It's just my prob with C with playing pointers etc too
Thanks a lot.