Hello, I am just going over tutorial on line and working on this character handling program from on line tutorial.
First of all, I just copy the program but it doesn't compile.
It complains that (int)cp-in_line is illegal
cha_handle.c:21: error: invalid operands to binary - (have âintâ and âchar *â)
Also, I don't understand why in_line(begining of array) need to be subtracted from current position in array(denoted by cp)...
Any help would be appreciated.
thank you.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINELNG 100 /* max length of input line */
main() {
char in_line[LINELNG];
char *cp;
int c;
cp = in_line;
while ( ( c=getc(stdin) ) != EOF ) {
if ( cp == &in_line[LINELNG-1] || c == '\n' ) {
*cp = 0;
if ( strcmp( in_line, "stop" ) == 0 )
exit( EXIT_SUCCESS );
else
/* printf("line was %d characters long\n", (int)cp-in_line); */
printf("line was %d characters long\n",
(int)cp-in_line);
cp = in_line;
}
else
*cp++ = c;
}
exit(EXIT_SUCCESS);
}