By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

code hangs

P: n/a
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? :-)

Jun 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
mdh wrote:
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

Jun 18 '06 #2

P: n/a
mdh

Xicheng Jia wrote:
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

Jun 18 '06 #3

P: n/a
On 2006-06-18, mdh <md**@comcast.net> wrote:
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. :-)

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 18 '06 #4

P: n/a

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

Jun 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.