Re: K&R2, exercise 4-2
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Fri, 04 Apr 2008 12:43:38 +0100
arnuld <looting@xxxxxxxxxxxx> writes:
On Thu, 03 Apr 2008 21:17:30 +0530, Ben Bacarisse wrote:
arnuld <oonga@xxxxxxxxxx> writes:
if( match_num )
{
printf("Position %d is the last to match\n\n", matched_idx);
}
}
... when there is no match, the output is "left hanging". Output that
does not end with a newline is guaranteed to appear on all systems.
I really don't get it at all. If there is no match then condition will
fail and hence othing to do.
No, you are right. I based my comment on an old version that
sometimes did this:
./arnuld
a term
^D
1 matches,
0 is the last to match$
The $ is my system's prompt. Your new fixed version prints nothing if
there is no match, but it takes a while to reason that out. You
should probably remove the test "if (match_num)" since it suggests
that a partial line can be printed. The test is redundant.
For simple testing, I prefer to read a whole line, storing only those
characters that fit but this way is fine. Except...
I am at chapter 4 where authors did not start discussing the "whole line"
concept except using a rudimentary function named getline() to do
that.
I was suggesting an alternative, that is all. You do, essentially,
this:
while (there-is-room && next-char-is-not-newline)
store-that-char;
I often write:
while (next-char-is-not-newline)
if (there-is-room)
store-that-char;
so that a whole line is always read, even when there is not room for
it all.
if( c == '\n' )
{
s[i++] = '\n';
}
}
s[i] = '\0';
... I think you have a bug here. The loop above can put max-1 chars
into the buffer (max being what it was originally, of course) but it can
end when c == '\n'. You then try to put the '\n' and the '\0' in the
one remaining space!
sorry, this function is just a copy of K&R2 example, as I said earlier.
so, something wrong with K&R2 ?
I'd love to be the one to find a bug in K&R2 after all those eyes have
looked at it but, no, K&R is correct. There are two getline functions
in the book (I have K&R first edition so I won't give you page
numbers). The first tests against i < lim-1 and the second has a test
for --lim > 0 *before* reading the next character. Your code is
slightly different and has the bug I described.
The moral is that you can't just change
for (i = 0; --max > 0 && (c = getchar()) != EOF && c != '\n'; ++i)
into
for (i = 0; (c = getchar()) != EOF && c != '\n' && --max > 0; ++i)
with no consequences.
Interestingly, both of K&R's getline functions exhibit undefined
behaviour when called with a buffer size of 1. This is daft enough
not to be a bug as such, but I would have preferred to avoid it in a
teaching text.
match_num = 0;
OK, maybe you do this for testing, but a utility function like this
should not use a file-scope (AKA "global") variable like match_num. Of
course, you code works if you just make match_num local to this
function.
I have used match_num in main() function and that is why I made it global.
If it is local then main() is not going to access it.
That is fine for now, but global variables are not a good way to
communicate with general-purpose functions like str_index. When you
have got further though the book, you'll find ways to get more
information out of a function that means you don't need global
variables like that.
--
Ben.
.
- References:
- K&R2, exercise 4-2
- From: arnuld
- Re: K&R2, exercise 4-2
- From: arnuld
- Re: K&R2, exercise 4-2
- From: Ben Bacarisse
- Re: K&R2, exercise 4-2
- From: arnuld
- Re: K&R2, exercise 4-2
- From: Ben Bacarisse
- Re: K&R2, exercise 4-2
- From: arnuld
- K&R2, exercise 4-2
- Prev by Date: Re: Reading BSD system code
- Next by Date: Re: Not quite standard... but socially acceptable
- Previous by thread: Re: K&R2, exercise 4-2
- Next by thread: would C be easier to read if...
- Index(es):
Relevant Pages
|