Qchem Posted March 6, 2005 Report Share Posted March 6, 2005 Hi guys, I'm trying to learn C from K&R and I'm having trouble with Exercise 1-18. #include <stdio.h> #define MAXLINE 1000 /* max line length */ int getline(char s[], int maxline); void remspac(char t[], int len); /* Remove trailing whitespace from lines & remove blank lines */ main() { int len; /* line length */ char line[MAXLINE]; while ((len = getline(line, MAXLINE)) > 0) ; remspac(line, len); return 0; } /* getline: reads a line into s, and returns the length (i) */ int getline(char s[], int lim) { int i, c; 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'; if (s[i-2] == ' ') printf("Trailing space\n"); return i; } void remspac(char t[], int len) { printf("%s", t); } The problem is that my character array "line" does not seem to be passed to the function remspac correctly - remspac (which will do more later) doesn't print anything!! Anyone any ideas what I'm doing wrong? I know pointers are an important concept in C, but they haven't been covered in the book so far so I don't want to use them. Thanks in advance, Grant Quote Link to comment Share on other sites More sharing options...
Qchem Posted March 7, 2005 Author Report Share Posted March 7, 2005 Gah, found my dumb mistake!! Theres a null operator right after the while loop that calls getline, it shouldn't be there! Quote Link to comment Share on other sites More sharing options...
xxbeanxx Posted March 7, 2005 Report Share Posted March 7, 2005 don't ya love finding your mistakes only after posting to a forum? :) happens to me all the time. Quote Link to comment Share on other sites More sharing options...
Cannonfodder Posted March 8, 2005 Report Share Posted March 8, 2005 Oh dude! Almost time for pointers! :lol: Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.