473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

question about an exercise in K&R (p96)

mdh
I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.

K&R say the example "break(s) a stream of characters into integer
values"

The relevant code is: ( I think)

(in Main)

for (i=0; i < SIZE && getint(&arr[i]) != EOF; i++);

and I believe the relevent code in getint is:

while ( isspace(c=getch()));

if ( !isdigit (c) && c != EOF && c != '+' && c != '-'){
ungetch(c);
return 0;

where getch() and ungetch() are K&R versions of pushing/getting the
extra character to/from a buffer.
My question is this:

If my input is something like: " 89 76 45 -90" the out put is the same
(if I print the array)

but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?

Thanks in advance.

Nov 15 '06 #1
15 1522
mdh said:
I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.
<snip>
but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?
It's not the intended input, that's for sure! Either K&R are sacrificing
robustness for clarity (which, in my view, is a mistake, but that's a
matter of opinion) - or they screwed up. Your call.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 15 '06 #2
Richard Heathfield wrote:
mdh said:
I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.
<snip>
but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?

It's not the intended input, that's for sure! Either K&R are sacrificing
robustness for clarity (which, in my view, is a mistake, but that's a
matter of opinion) - or they screwed up. Your call.
One would've thought a bug in K&R's exercises would've been spotted
before now. It would help if mdh posted the full source, as my copy of
K&R isn't at hand right now.

Nov 15 '06 #3
santosh said:
Richard Heathfield wrote:
>mdh said:
I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.
<snip>
but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?

It's not the intended input, that's for sure! Either K&R are sacrificing
robustness for clarity (which, in my view, is a mistake, but that's a
matter of opinion) - or they screwed up. Your call.

One would've thought a bug in K&R's exercises would've been spotted
before now.
It isn't a bug as such. The program works fine when presented with
appropriate input, and this is, after all, an exercise in pointers as
function arguments! K&R may well have considered a more robust driver to be
a distraction. In fact, they don't even bother to present a complete
driver. The code the OP as quoted as being from main() is in fact just a
four-line code fragment.
It would help if mdh posted the full source,
What he posted, pretty much, is all the code they wrote for this, although
he appears to have done it from memory, as there are one or two
discrepancies.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 15 '06 #4
mdh

Richard Heathfield wrote:
>
It would help if mdh posted the full source,

What he posted, pretty much, is all the code they wrote for this, although
he appears to have done it from memory, as there are one or two
discrepancies.

Well, that is what I wondered...although I have looked over it again
and again. Here is the full code...somewhat altered, mainly so I could
understand it.

>>>>>>>>>>>>
#include <stdio.h>
# define SIZE 10

int main (){

int getint( int *ptr );
int arr[SIZE], i;

printf("Write a sentence with a bunch of numbers:\n\n");

for (i=0; i < SIZE && getint(&arr[i]) != EOF; i++);

printf("\nThe following numbers were scanned\n\n");

for (--i; i >= 0; i--)
printf( "%d \n", arr[i] );

return 0;
}
/*******int getint( int *arr ) ******/
#include <ctype.h>
int getint( int *ptr ){

void ungetch( int );
int getch(void);

int sign, c;

while ( isspace(c=getch())); /**ignores white spaces ****/

/** now filter for a number **/

if ( !isdigit (c) && c != EOF && c != '+' && c != '-'){
ungetch(c);
return 0;
} /** this is not a number **/

/*** filtered for EOF, +, -, number ***/
sign = (c=='-')? -1: 1;
if (c=='+' || c == '-')
c=getch();

for ( *ptr=0; isdigit(c); c=getch())
*ptr=10* *ptr + ( c-'0');

*ptr *= sign;
if ( c != EOF)
ungetch(c);
return c;
}
/*******void ungetch( int )******/
/******int getch(void)******/

#define SIZE2 50
int buffer[SIZE2], buffpos=0;

void ungetch( int c ){

if ( buffpos >= SIZE2)

printf ( "Error: Buffer Overflow");

else

buffer[buffpos++]=c;

}

int getch(void){

return (buffpos 0) ? buffer[--buffpos]: getchar() ;

}

<<<<<<<<<<<<

Nov 15 '06 #5
mdh

mdh wrote:
Here is the full code:

Here is some input/output I obtained:
>>>>>
Write a sentence with a bunch of numbers:

34 67 -90


The following numbers were scanned

-90
67
34

ptrs has exited with status 0. <<=== Seems to work ok if input is
ONLY digits

<<<<<<<<<

Write a sentence with a bunch of numbers:

run 89

The following numbers were scanned

0
0
0
1
0
-1881117246
1
0
2036621151
115

ptrs has exited with status 0. <<<== Not quite sure what is going on
here...hence my post.

<<<<<<<<<<<<

Nov 15 '06 #6
mdh said:
>
Seems to work ok if input is ONLY digits
As I have already explained, K&R only expect you to provide digits in your
test data. Their emphasis here is on learning how to use pointers to tell
functions where arrays live. They have skimped (wrongly, in my view, but
that's a matter of opinion) on error-checking in their skeleton driver.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 15 '06 #7
mdh

Richard Heathfield wrote:
mdh said:

Seems to work ok if input is ONLY digits

As I have already explained, K&R ..........

Just wanted to make sure. Thank you for your input...it is always
appreciated.

Nov 15 '06 #8
"Richard Heathfield" <in*****@invalid.invalidwrote in message
news:tP********************@bt.com...
mdh said:
>I am just trying to figure out if this is intended behavior, or whether
I am missing something...(probably the latter).

The example given (p96, K&R) is meant to illustrate pointer behavior.
<snip>
>but as soon as I introduce a character other than a digit, it goes into
an endless loop, and exits when

i < SIZE is false.

Is this the intended behavior?

It's not the intended input, that's for sure! Either K&R are sacrificing
robustness for clarity (which, in my view, is a mistake, but that's a
matter of opinion) - or they screwed up. Your call.

That's what I thought a few years back:

http://groups.google.com/group/comp....c19714cdd8795d

Conclusion: "It's just the example program that could be more robust."

--
Tim Hagan
Nov 15 '06 #9
mdh

Tim Hagan wrote:
>
That's what I thought a few years back:

Good to know that the same confusion existed before!!! :-)
I still love K&R. Best of the books I have read. Plus, this group is
great...incredibly helpful.

Nov 15 '06 #10
mdh

Richard Heathfield wrote:
As I have already explained, K&R only expect you to provide digits in your
test data.
A quick question for Richard.
The first exercise after this small example given by K&R, asks the
reader to "fix" the example so that a "+" or a "-" NOT followed by a
digit is correctly pushed back to the input. K&R seem to imply that the
input might not only be digits. Up to now, I have found all their
examples to be spot on with no ambiguity...perhaps this is the first
one?

Nov 15 '06 #11
mdh wrote:
Richard Heathfield wrote:
As I have already explained, K&R only expect you to provide digits in your
test data.

A quick question for Richard.
The first exercise after this small example given by K&R, asks the
reader to "fix" the example so that a "+" or a "-" NOT followed by a
digit is correctly pushed back to the input. K&R seem to imply that the
input might not only be digits. Up to now, I have found all their
examples to be spot on with no ambiguity...perhaps this is the first
one?
I don't see any ambiguity. Their example omits rigorous error checking
for the sake of brevity, (as the authors themselves explain once or
twice in the book), but the exercises are meant to help you think by
often asking _you_ to extend their examples. Here, they're asking you
to incorporate rudimentary input checking and take appropriate action
upon encountering unexpected input.

Nov 15 '06 #12
mdh

santosh wrote:
I don't see any ambiguity. ........
>....... Here, they're asking you
to incorporate rudimentary input checking and take appropriate action
upon encountering unexpected input.

Ok...the difference is that when you guys ( the experienced ones :-) )
look at the issue, it is obvious what they are saying. When one is
learning it, one often wonders what one is missing!
I guess that's what makes this list so helpful.
Your input is appreciated...thanks.

Nov 15 '06 #13
mdh wrote:
santosh wrote:
I don't see any ambiguity. ........

....... Here, they're asking you
to incorporate rudimentary input checking and take appropriate action
upon encountering unexpected input.


Ok...the difference is that when you guys ( the experienced ones :-) )
look at the issue, it is obvious what they are saying. When one is
learning it, one often wonders what one is missing!
I guess that's what makes this list so helpful.
Your input is appreciated...thanks.
If increasing distance from the Sun were directly proportional to
experience with C, then probably you'd be Proxima Centauri, I'd be
Alpha Centauri while Richard might be Deneb. :)

Nov 15 '06 #14
mdh

santosh wrote:

If increasing distance from the Sun were directly proportional to
experience with C, then probably you'd be Proxima Centauri, I'd be
Alpha Centauri while Richard might be Deneb. :)
LOL...PLEASE let it take less than 4.22 years for some of the light of
C to reach me!!!

Nov 15 '06 #15
mdh said:
>
Richard Heathfield wrote:
>As I have already explained, K&R only expect you to provide digits in
your test data.

A quick question for Richard.
The first exercise after this small example given by K&R, asks the
reader to "fix" the example so that a "+" or a "-" NOT followed by a
digit is correctly pushed back to the input. K&R seem to imply that the
input might not only be digits. Up to now, I have found all their
examples to be spot on with no ambiguity...perhaps this is the first
one?
You appear to have missed Exercise 5-1A, which reads:

"By now, you've probably discovered other ways to confuse the code. Fix it."
:-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 15 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
12
by: Merrill & Michele | last post by:
It's very difficult to do an exercise with elementary tools. It took me about fifteen minutes to get exercise 1-7: #include <stdio.h> int main(int orange, char **apple) { int c; c=-5;...
8
by: Mike S | last post by:
Hi all, I noticed a very slight logic error in the solution to K&R Exercise 1-22 on the the CLC-Wiki, located at http://www.clc-wiki.net/wiki/KR2_Exercise_1-22 The exercise reads as...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.