Hi all,
I'm new to the boards and new to C, working my way through K&R and I've become stuck on exercise 1-18. I've used a function to place an input line into a character array named line. main then attempts to pass the array to a new function, which is currently supposed to simply print the string (it'll do more later) - yet the string doesn't print.
I suspect that the array in the second function isn't pointing to the correct mem location but I can't see what I'm doing wrong. I'd really appreciate any tips!!
Code:
#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);
}