question about an exercise in K&R (p96)

M

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) != 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.
 
R

Richard Heathfield

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.
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.
 
S

santosh

Richard said:
mdh said:



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.
 
R

Richard Heathfield

santosh said:
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.
 
M

mdh

Richard said:
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) != EOF; i++);

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

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

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() ;

}

<<<<<<<<<<<<
 
M

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.

<<<<<<<<<<<<
 
R

Richard Heathfield

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.
 
M

mdh

Tim said:
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.
 
M

mdh

Richard said:
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?
 
S

santosh

mdh said:
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.
 
M

mdh

santosh said:
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.
 
S

santosh

mdh said:
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. :)
 
M

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!!!
 
R

Richard Heathfield

mdh said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

K&R p.97 8
K&R 5-1 9
Understanding an exercise about sub-sums in arrays 1
K & R new edition? 10
K&R Exercise 1-21: entab 10
Questions about K&R (Kernighan and Ritchi) 57
GCC/K&R Incompatibility 16
Alignment (K&R question) 13

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top