code hangs

M

mdh

They say one learns more from one's errors...and I am certainly keeping
that up. The following is code from K&R explaining external
variables...but that is not directly relevant here. When I wrote it, I
initially wrote the code immediately below. But this hangs.

(c is an integer, which may or may not contain a digit. s is a
character array and getch() is a function which gets the next
character)

if ( isdigit(c) )
while ( s[++i] = c = isdigit(getch()));


When I went back and finally saw what I perceived to be a small
difference, the code worked as intended.



if ( isdigit(c) )
while (isdigit ( s[++i] = c = getch()));

I have not included all the code, as I do not believe it is relevant.
Is this what is happening in the first incorrect code...?


When getch() returns a non-digit, the code stops just as it is supposed
to do, thus prevents assignment of c and s[] so the next line of code
is never reached, or is there something else which I have missed as
usual? :)
 
X

Xicheng Jia

mdh said:
They say one learns more from one's errors...and I am certainly keeping
that up. The following is code from K&R explaining external
variables...but that is not directly relevant here. When I wrote it, I
initially wrote the code immediately below. But this hangs.

(c is an integer, which may or may not contain a digit. s is a
character array and getch() is a function which gets the next
character)

if ( isdigit(c) )
while ( s[++i] = c = isdigit(getch()));

coz s[++i] = c = isdigit(getch()) is an assignment expression instead
of logic expression, and it's always TRUE except the assigned value is
zero. it's exact the same as you ask why it's different between the
following two lines:

while (x = y) { ... }
while (x==y) { ... }

HTH
Xicheng
 
M

mdh

Xicheng said:
coz s[++i] = c = isdigit(getch()) is an assignment expression instead
of logic expression, and it's always TRUE except the assigned value is
zero. it's exact the same as you ask why it's different between the
following two lines:

while (x = y) { ... }
while (x==y) { ... }

HTH

Sure does...nice insight for me.
Thanks
 
A

Andrew Poelstra

They say one learns more from one's errors...and I am certainly keeping
that up. The following is code from K&R explaining external
variables...but that is not directly relevant here. When I wrote it, I
initially wrote the code immediately below. But this hangs.

(c is an integer, which may or may not contain a digit. s is a
character array and getch() is a function which gets the next
character)

if ( isdigit(c) )
while ( s[++i] = c = isdigit(getch()));

You place either a 0 or a !0 into s[++i]. I can't imagine that that is
what you intented.
When I went back and finally saw what I perceived to be a small
difference, the code worked as intended.

Hardly a "small difference".
if ( isdigit(c) )
while (isdigit ( s[++i] = c = getch()));

Now you have assigned the return value of getch() to both c and s[++i],
which is a char. Now, instead of placing a bunch of 0's and 1's into a
string of chars, you've placed character digits.
I have not included all the code, as I do not believe it is relevant.
Is this what is happening in the first incorrect code...?

I can't imagine what the rest of the code looks like, but no, it is not
relevant that I can see.
When getch() returns a non-digit, the code stops just as it is supposed
to do, thus prevents assignment of c and s[] so the next line of code
is never reached, or is there something else which I have missed as
usual? :)

Something else. :)
 
S

spibou

mdh said:
When I wrote it, I
initially wrote the code immediately below. But this hangs.

Meaning what exactly ?
(c is an integer, which may or may not contain a digit. s is a
character array and getch() is a function which gets the next
character)

if ( isdigit(c) )
while ( s[++i] = c = isdigit(getch()));


When I went back and finally saw what I perceived to be a small
difference, the code worked as intended.



if ( isdigit(c) )
while (isdigit ( s[++i] = c = getch()));

I have not included all the code, as I do not believe it is relevant.
Is this what is happening in the first incorrect code...?


When getch() returns a non-digit, the code stops just as it is supposed
to do, thus prevents assignment of c and s[] so the next line of code
is never reached, or is there something else which I have missed as
usual? :)

The part of the code you do include does not give me an idea as to
why it hangs. One thing to consider is what happens if getch() keeps
returning digits ? Eventually you will go beyond the bounds of s[].

Best to come up with a short *complete* piece of code which exhibits
the problem and post it here.
 

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

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top