Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Program to print the longest line |
| Date | 2015-11-18 18:10 +0000 |
| Organization | PANIX Public Access Internet and UNIX, NYC |
| Message-ID | <n2ievi$g9$1@reader1.panix.com> (permalink) |
| References | <8a605c3e-2e2d-456f-bcf4-f23bf4c40bc2@googlegroups.com> |
In <8a605c3e-2e2d-456f-bcf4-f23bf4c40bc2@googlegroups.com> Alla _ <modelling.data@gmail.com> writes:
> int getline (char s[], int lim);
> // this is how this line looked before I changed it:
> int getline{char line[], int maxline);
Is that really how it looked? The "{" is likely your typo. Please do not
type code in from memory.
Typos aside, the only difference between those two prototypes is in
the names of the arguments, which does not matter here.
Since you are including stdio.h, you may be conflicting with a library
function also named getline(). Please consider renaming this function.
(This may the root of your problem, although if so, I'm puzzled why you
did not receive a compilation error.)
> Do I understand correctly the following:
> 1) although the array "line" has yet nothing in it, when the array name
> "line" is passed into getline() function, the function receives an
> address to the first character of the array; hence the function "knows" where
> to store its input; therefore, an empty array is passed to the function,
> or to be more precise, the address of an empty array to be filled in by
That's correct, with the exception that "line" is not empty. You haven't
stored anything meaningful in it yet, but that's not the same.
> If yes, then it's not clear to me. Getline() returns only one i, hence
> len is always equal to that i (the condition of no input is never even
> checked), and is always bigger than 0. How does the loop ever terminate?
No, len is not always bigger than zero. If the function is called after
end of input has already been reached, the string is immediately
null-terminated at zero length, and zero is returned.
> And, I don't see any update of the while loop; what is the base for iteration?
The loop continues executing as long as getline() returns a nonzero value.
> // I am flummoxed by this condition:
> i < lim - 1 && (c = getchar()) != EOF && c != '\n'
> = until i is less then MAXLINE AND the EOF is not valid AND ENTER has not
> been hit.
> Shouldn't || OR be instead of && as in:
> i < lim - 1 && c != '\n' || (c = getchar()) != EOF
No, it should not be OR. If written this way, the loop would never
terminate, because no matter what value c has, it would necessarily be
unequal to \n or unequal to EOF. (If c is \n then it's unequal to
EOF; if it's EOF then it's unequal to \n; and any other value is unequal
to both.)
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-18 08:10 -0800
Re: Program to print the longest line Keith Thompson <kst-u@mib.org> - 2015-11-18 08:49 -0800
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-18 09:03 -0800
Re: Program to print the longest line Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-18 09:04 -0800
Re: Program to print the longest line Keith Thompson <kst-u@mib.org> - 2015-11-18 09:24 -0800
Re: Program to print the longest line Richard Heathfield <rjh@cpax.org.uk> - 2015-11-18 17:42 +0000
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-18 10:49 -0800
Re: Program to print the longest line James Kuyper <jameskuyper@verizon.net> - 2015-11-18 13:55 -0500
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-18 11:01 -0800
Re: Program to print the longest line James Kuyper <jameskuyper@verizon.net> - 2015-11-18 14:19 -0500
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-18 21:50 -0800
Re: Program to print the longest line James Kuyper <jameskuyper@verizon.net> - 2015-11-19 06:39 -0500
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-19 05:07 -0800
Re: Program to print the longest line James Kuyper <jameskuyper@verizon.net> - 2015-11-19 10:12 -0500
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-19 07:49 -0800
Re: Program to print the longest line Richard Heathfield <rjh@cpax.org.uk> - 2015-11-18 19:08 +0000
Re: Program to print the longest line Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-18 14:54 -0500
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-18 23:36 -0800
Re: Program to print the longest line Keith Thompson <kst-u@mib.org> - 2015-11-19 21:15 -0800
Re: Program to print the longest line Geoff <geoff@invalid.invalid> - 2015-11-19 21:29 -0800
Re: Program to print the longest line David Brown <david.brown@hesbynett.no> - 2015-11-20 11:00 +0100
Re: Program to print the longest line glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-20 15:13 +0000
Re: Program to print the longest line supercat@casperkitty.com - 2015-11-20 11:29 -0800
Re: Program to print the longest line Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-20 12:51 -0800
Re: Program to print the longest line Philip Lantz <prl@canterey.us> - 2015-11-24 01:59 -0800
Re: Program to print the longest line Tim Rentsch <txr@alumni.caltech.edu> - 2015-11-27 13:34 -0800
Re: Program to print the longest line John Gordon <gordon@panix.com> - 2015-11-18 18:10 +0000
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-18 10:53 -0800
Re: Program to print the longest line Barry Schwarz <schwarzb@dqel.com> - 2015-11-18 13:55 -0800
Re: Program to print the longest line Alla _ <modelling.data@gmail.com> - 2015-11-18 23:32 -0800
Re: Program to print the longest line Barry Schwarz <schwarzb@dqel.com> - 2015-11-18 11:58 -0800
csiph-web